How load Adsense using Jquery - javascript

I'm trying to dynamically load Adsense at a specific point on my Wordpess posts - just before a div class that is manually inserted within the wysiwyg text area on each post. So far, I've used the following ...
<?php wp_enqueue_script( 'jquery' ); ?>
before wp_head, and after:
<script type="text/javascript">
jQuery(document).ready(function($){
$(".example-div").before("Here is the content");
});
</script>
In the above example plain text like "here is the content" loads in the desired position, but it doesn't work when I insert the Adsense code.
<script type="text/javascript">
jQuery(document).ready(function($){
$(".example-div").before("<script type="text/javascript"><!--
google_ad_client = "ca-pub-************";
/* Ad ID */
google_ad_slot = "********";
google_ad_width = 320;
google_ad_height = 50;
//-->
</script>
<script type="text/javascript"
src="//pagead2.googlesyndication.com/pagead/show_ads.js">
</script>");
});
</script>
Any suggestions on what modficiations are required - or best way to make the Adsense code appear - greatly appreciated.

Related

How to add Javascript after closing /body tag in WordPress

I am trying to embed an iFrame within several WordPress pages. Each page will use a different iFrame content. This is for a commerce site so needs the first iframe to be the customer's login. Subsequent iframes on different WP pages will be for product search, checkout... ((my question is at the bottom))
In the header.php I placed some initial code that i need to be included on every WordPress page.
I added the iframe within each page's body.
I need to initialize the iframe to a specific iframe content. I would like to place this code AFTER the closing </body> tag as follows:
<html>
<head>
<script>
var absDomain = "http://www.webstore.com/"
var iframeObj;
var timestampString = '' + new Date().getTime() + ''
document.write('<script type="text/javascript" src="' + absDomain + 'Support.js?'+timestampString+'"><\/script>');
document.write('<script type="text/javascript" src="' + absDomain + 'ProductSearch.js?'+timestampString+'"><\/script>');
document.write('<script type="text/javascript" src="' + absDomain + 'Environment.js?'+timestampString+'"><\/script>');
function gotoIframe(theLocation){
var timeStamp = "";
iframeObj = document.getElementById("iframeContent");
timeStamp = "&" + new Date().getTime();
iframeObj.src = absDomain + theLocation + "?iframe=yes" + timeStamp;
}
function executeIFrameProductSearch(str1, str2){
goTo_CategoryForIframe(str1, str2, document.getElementById("iframeContent"));
}
</script>
</head>
<body>
<!-- iFrame embedded below -->
<iframe id="iframeContent" style="width:100%;height:500px; " src=""></iframe>
</body>
<script>
//script needed for initial iframe page and iframe support.
iframeObj = document.getElementById("iframeContent");
gotoIframe("CustomerLogin.html");
</script>
</html>
//After the above code section (after the </body> tag) is embedded into the Login page, I expect to use:
//Customer Login:
Customer Login
//Product Search:
Product Search
Question: Where do I insert the code that's AFTER the closing tag for each of my specific WordPress pages? Thanks.
It is invalid to put script tag after body tag, but if you really want to do that,
you can look into the theme's footer.php.
For example, you may find code like this in footer.php:
<?php
</div><!-- close tag for content container -->
?>
<footer>
<!-- footer content -->
</footer>
</body>
<!-- here is the place to put your code -->
</html>
Copy and paste this function in your functions.php file
//alert(); enable alert to test
//script needed for initial iframe page and iframe support.`function your_function() {
echo '<script>`
iframeObj = document.getElementById("iframeContent");
gotoIframe("CustomerLogin.html");
</script>';
}
add_action( 'wp_footer', 'your_function', 100 ); // this will add the script after the body tag for every pages
other option will be
// Rest of your functions.php above this line or wherever!
function yourprefix_scripts_after_opening_body_tag() {
$javascript = file_get_contents( get_template_directory() . '/js/opening-body.js' );
echo '' . $javascript . '';
}
add_action( 'yourprefix_after_opening_body_tag', 'yourprefix_scripts_after_opening_body_tag' );
paste this code in footer.php where you want to display

Display Google AdSense ads to search engine traffic using html javascript

I am using below PHP code for Displaying AdSense Ads only for search engine visitor.
But now I need HTML/JavaScript Code for Displaying Ads on search engine visitor.
I'm trying to create HTML/JavaScript code, but failed.
Can I modify/create this HTML/JavaScript code?
Have any solution? or Have any HTML/JavaScript code?
<?php
$referrer = $_SERVER['HTTP_REFERER'];
$my_domain = "example.com";
$search_engines = "google|yahoo|bing|altavista|digg";
$pattern = "((http(s)?://)(\w+?\.)?(?!{$my_domain})({$search_engines}))";
if (preg_match("/{$search_engines}/i", $referrer) != false) {
echo <<<END
<script type="text/javascript"><!--
google_ad_client = "xx-xx-xxxxxxxxxxxxxxxxxx";
/* xxxxxxxx xxxxxx xxx xxx xxx xx xxxxxx */
google_ad_slot = "xxxxxxxxxxxxxx";
google_ad_width = xxx;
google_ad_height = xxx;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
END;
} else {
// Show something to visitors not referred by a search engine
}
?>
Please help me, Sir.
It's a little bit stupid, but you definetely can translate it to js:
<script type="text/javascript">
if (document.referrer.match(/google|yahoo|bing|altavista|digg/)) {
google_ad_client = "xx-xx-xxxxxxxxxxxxxxxxxx";
/* xxxxxxxx xxxxxx xxx xxx xxx xx xxxxxx */
google_ad_slot = "xxxxxxxxxxxxxx";
google_ad_width = xxx;
google_ad_height = xxx;
var script = document.createElement("script");
script.setAttribute("src", src);
document.getElementsByTagName("head")[0].appendChild(script);
} else {
// Show something to visitors not referred by a search engine
}
</script>

Next and previous button using jquery

I am using a PHP Simple HTML DOM Parser to save a website as a htm file. I then use jQuery to extract a div from the file and put it into a div. I want to create a
Previous and Next button but the problem I have is that to do this I have to change the URL so the parser can get the page. I would really appreciate if you can help. Below is the code I am using.
<?php
include( 'site/simple_html_dom.php');
$html=file_get_html( 'http://roosterteeth.com/home.php?page=1');
$html->save('site/rtnews.htm')
?>
<script type="text/javascript" src="site/jquery.js"></script>
<script type="text/javascript">
$('document').ready(function() {
$('#wrap').click(function (event){
event.preventDefault();
});
$("#wrap").load('site/rtnews.htm #postsArea');
});
</script>
</head>
<body>
<div id="wrap">
</div>
</body>
You will have to create a new php file for this and make an AJAX request to that file. I assume you have already realised that you cannot make a cross-domain request due to CORS.
Here is your new php file, let's call it proxy.php. It will proxy the request, responding with the page that is passed to it via GET :
<?php
include( 'site/simple_html_dom.php');
$html=file_get_html( 'http://roosterteeth.com/home.php?page=' . $_GET["page"]);
echo $html;
?>
Your new JavaScript;
$('document').ready(function() {
var $wrap = $('#wrap'),
page = 1;
$('#next').on('click', function () {
getPage(++page);
});
$('#prev').on('click', function () {
getPage(--page);
});
var getPage = function (page) {
$wrap.load('proxy.php?page=' + page + ' #postsArea');
};
getPage(page);
});

How to remove window.alert pop up?

Here is some script I copied to create a mailto link which grabs the browser URL and inserts it into the subject of the email. Its working perfect but every page I go to a Javascript alert box pops up saying:
mailto:address#address.ca?Subject="WHAT EVER IS IN THE BROWSER ADDRESS BAR"
Im rubbish with JS. How do I get the script to work without the alert pop up or even so with a lifespan?
<script type='text/javascript' src='http://code.jquery.com/jquery-1.7.1.js'>
</script>
<script type='text/javascript'>
//<![CDATA[
$(window).load(function(){
var url = "mailto:address#address.ca?Subject=" + window.location;
$('#mailtoLink').attr('href', url);
window.alert($('#mailtoLink').attr('href')); // = url;
});//]]>
</script>
HERE IS A EXAMPLE PAGE:
http://www.door9.co.uk/bex/his-thoughts/
Just remove this line window.alert($('#mailtoLink').attr('href')); // = url; so you're left with:
<script type='text/javascript' src='http://code.jquery.com/jquery-1.7.1.js'>
</script>
<script type='text/javascript'>
//<![CDATA[
$(window).load(function(){
var url = "mailto:address#address.ca?Subject=" + window.location;
$('#mailtoLink').attr('href', url);
});//]]>
</script>
Just remove the window.alert function call.
https://developer.mozilla.org/en-US/docs/DOM/window.alert
Take out this line; it was clearly for demo purposes only
window.alert($('#mailtoLink').attr('href')); // = url;

How do you automatically refresh part of a page automatically using AJAX?

$messages = $db->query("SELECT * FROM chatmessages ORDER BY datetime DESC, displayorderid DESC LIMIT 0,10");
while($message = $db->fetch_array($messages)) {
$oldmessages[] = $message['message'];
}
$oldmessages = array_reverse($oldmessages);
?>
<div id="chat">
<?php
for ($count = 0; $count < 9; $count++) {
echo $oldmessages[$count];
}
?>
<script language="javascript" type="text/javascript">
<!--
setInterval( "document.getElementById('chat').innerHTML='<NEW CONTENT OF #CHAT>'", 1000 );
-->
</script>
</div>
I'm trying to create a PHP chatroom script but I'm having a lot of trouble getting it to AutoRefresh
The content should automatically update to , how do you make it do that? I've been searching for almost an hour
I would take that PHP functionality you have and putting it in a sperate page that returns JSON. From there you can call that method using jQuery and the AJAX tools built in. Really simple. Start here for jQuery: http://api.jquery.com/category/ajax/
you'll need to set up a server side script that renders only the contents of the chat div and use ajax to grab that. it can be done with jquery quite easily:
In your html document:
<head>
...
<script src="/path/to/jquery.js" type="text/javascript"></script>
<script>
var chatUpdateInterval = null;
function initChat() {
chatUpdateInterval = window.setInterval(updateChat, 5000); /* every 5 seconds */
}
function updateChat() {
$.ajax({
url: '/url/path/to/your/script.php'
,dataType: 'HTML'
,success: function(data, status, xhr){
$('#chat').append($(data).html());
}
});
}
$(document).ready(function(){
initChat();
});
</script>
...
</head>
<body>
<div id="chat">
please stand by while we're firing up the coal!
</div>
</body>
Note that this won't be really good, it's just a sample to get you started. you should look into jquery's $.ajax

Categories

Resources