JavaScript functions

Some ad optimizations, especially those that are cache-resistant, need to be implemented directly in your ad code. If you are familiar with your ad tags and with JavaScript, you can use the functions on this page to perform some actions. Most ad networks allow altering their ad tags, but you should check with them before.

Activate advanced JS functions

The advanced functions are not enabled by default to not effect speed on sites that don’t make use of these functions. To enable the functions go to Ads > Settings and enable the option Use advanced JavaScript.

advanced javascript option

max_per_session()

Use the function advads.max_per_session(name, max)  to display a banner only {max} times per session.

params

name – {string} name of the variable that is stored. can be anything id-like (upper and lower case, hyphen and underscore

max – {int} the number of times per session the function can return true

return

true if the maximum number is not reached yet

example

In this example, the alert message (a.k.a our ad) is displayed only on the first 3 page views of the current session.

<script>
if(!advads.max_per_session('my-ad', 3))
alert('display me');
</script>

Use the function advads.get_cookie(name)  to get the value of a cookie.

params

name – {string} name of cookie

return

value of the cookie

Use the function advads.set_cookie(name, value, exdays, path, domain, secure) to get the value of a cookie.

params

name – {string} name of cookie

value – {string} value of the cookie

exdays – {int/null} number of days until the cookie expires; use 0 to expire right away or null to expire with the session

path – optional: path the cookie is for

domain – optional

secure – optional

example

In this example, we first check if the advanced javascript class exists and if so, we set a cookie called sample_cookie with the value true and an expiry date in 60 days.

<script>
if(typeof advads !== 'undefined'){ 
advads.set_cookie('sample_cookie', true, 60); 
}
</script>

Use the function advads.cookie_exists(name)  to check if there is a cookie set.

params

name – {string} name of cookie

example

This example first checks if the advanced javascript class and the cookie example_cookie exists. If so you can perform some javascript magic.

<script>
if(typeof advads !== 'undefined' && advads.cookie_exists('example_cookie')){ 
// do whatever you need to do here
}
</script>

Make it better

Increase your ad management skills without spending more time.
Join over 150,000 publishers and AdOpts increasing their ad revenue.