Write JavaScript code inside JavaScript and all in PHP? - javascript

I have an ad that will appear when certain conditions, and when that ad appears, the ad will detect the width of the screen and use the ad size that I specify.
I did this using JavaScript, and inside JavaScript and all are in PHP. Problems arise when writing in document.write.
Could you please help me solve this problem.
This is my code:
<?php
if(!empty($variable1)) {
echo '
<script type="text/javascript">
var swidth = screen.width;
if (swidth >= 1218) {
document.write('
<script type="text/javascript">
document.write("
<script type='text/javascript'>
var adcode = {abcdefg};
</script>
<script type='text/javascript' src='http://domainname.com/ads.js'></script>
");
</script>
');
}
</script>
';
} else {
if(!empty($variable2)) {
echo $ads2;
} else {}
}
?>

from line 13 to 22 replace:
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'http://domainname.com/ads.js';
$("body").append(script);
var script2 = document.createElement('script');
script2.type = 'text/javascript';
script2.src = 'sc.js';// or url to the script file;
$("body").append(script2);
and sc.js:
var adcode = {abcdefg};
BUT
The best way to add JS in PHP:
<?php
// ...
?>
<script src="url_to_script"></script>
<?php
// ...
?>
Manipulations with DOM elements must be defined in JS and not affect PHP.

Related

Replace a string with a javascript code and load it (everything after pageload)

I'm hosting my blog/website on blogger, I need to insert an ad above any first chapter of any post. I can't modify any single post. I can't place the ad code in the theme html editor just before the post content, because I already have another ad before post content body.
I try with this 2 codes but none of them work:
1)
<script>
document.body.innerHTML = document.body.innerHTML.replace('<div class="description"', "<div id='toppostrect300x250' class='description'");
var container = document.getElementById('toppostrect300x250');
var w = document.write; document.write = function (content) {
container.innerHTML = content;
document.write = w; };
var script = document.createElement('script'); script.type = 'text/javascript'; script.src = '//p252740.clksite.com/adServe/banners?tid=252740_484260_6'; document.body.appendChild(script);
</script>
2)
<script>
document.body.innerHTML = document.body.innerHTML.replace('<h3>Chapter 1', "
<script data-cfasync='false' type='text/javascript' src='//p252740.clksite.com/adServe/banners?tid=252740_484260_6'><\/script>");
</script>

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>

Javascript src with variable

I need to embed a player by the following code:
<script type="text/javascript" src=***the url***></script>
A part in the url is the ID which is retrieved from mySQL database. I know that the string concatenation can't be directly handled in the src, so I add the function in the beginning:
<head>
<script>
function call(url)
{
var script = document.createElement('script');
script.type="text/javascript";
script.src = url;
document.body.appendChild(script);
}
</script>
</head>
In the body,
<body>
<?php ***...to retrieve ID***?>
<script type="text/javascript">
var videoid = "<?php echo $id; ?>"; //Transfer ID to javascript
var url="http://......"+videoid; //Url string finished
call(url); //Function call
</script>
Now the script is appended, but I can't see the player. Why?

Inserting <script>codehere</script> in header via JS in the body?

http://jsfiddle.net/8wGRg/
<script>
document.getElementsByTagName("head")[0].appendChild("<scr" + "ipt>alert('Hi!');</scr" + "ipt>");
</script>
I have a PHP string which contains JS code. For the sake of this code, lets say my string is <script>alert('Hi!');</script>. Within the body of my HTML document, how can I use JS to insert the JS string into the head.
As you can see, I'm breaking the word script into two parts so that it doesn't actually process it in the body before pushing it to the head.
The PHP string contains this:
<script type='text/javascript'>
var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];
(function() {
var gads = document.createElement('script');
gads.async = true;
gads.type = 'text/javascript';
var useSSL = 'https:' == document.location.protocol;
gads.src = (useSSL ? 'https:' : 'http:') +
'//www.googletagservices.com/tag/js/gpt.js';
var node = document.getElementsByTagName('script')[0];
node.parentNode.insertBefore(gads, node);
})();
</script>
<script type='text/javascript'>
googletag.cmd.push(function() {
googletag.defineSlot('/1019006/ba_ad_footer_728x90', [728, 90], 'div-gpt-ad-1378123123651-0').addService(googletag.pubads());
googletag.pubads().enableSingleRequest();
googletag.enableServices();
});
</script>
PHP code to generate JS:
<?php $header_code = $GLOBALS['settings']['header_code']; // just an example of how I load the code ?>
JS to insert the PHP
document.getElementsByTagName("head")[0].appendChild("<?php echo $header_code; ?>");
It doesn't append it because you are trying to append a string instead of a DOM node.
var s = document.createElement('script');
s.appendChild(document.createTextNode('alert("Hi!")'));
document.getElementsByTagName("head")[0].appendChild(s);

javascript and jquery override

I am making a small project that will let:
different websites to load my external javascript file: something like this:
<script type="text/javascript">
var vsid = "SA89696";
(function() {
var vsjs = document.createElement('script'); vsjs.type = 'text/javascript'; vsjs.async = true; vsjs.setAttribute('defer', 'defer');
vsjs.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'example.com/robot/robot.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(vsjs, s);
})();
</script>
now I want to use jquery in my javascript file, I dont have problem to load it by myself,
but I dont want to cause errors to the curresnt website if it already loaded jquery.
do you have any suggestions how to load jquery with no overrides.
You need something like this:
<script>
if(!window.jQuery)
{
var script = document.createElement('script');
script.type = "text/javascript";
script.src = "path/to/jQuery";
document.getElementsByTagName('head')[0].appendChild(script);
}
</script>
It checks if jQuery is loaded or no..
Source

Categories

Resources