I have a code that i want to execute but dont know how to go about it. I have a page that you can convert between two languages, english and french. I want to run a script in my index.html.erb file only if the page is in french and not in English.
This code is at the beginning of my index.html.erb file.
<script>
var a = <%= params[:locale]%>
if(a == "fr")
{
<script type="text/javascript">
/* <![CDATA[ */
var google_conversion_id = 1001007867;
/* ]]> */
</script>
<script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js">
</script>
<noscript>
<div style="display:inline;">
</div>
</noscript>
}
</script>
I am not sure if my code is correct. Any help is appreciated.
you cant nest script tags, you could append a script tag if your condition is met, sth. like :
<script>
var a = <%= params[:locale]%>;
console.log(a) /* THIS REALLY LOGS THE EXPECTED VALUE ? */
if(a == "fr")
{
var google_conversion_id = 1001007867;
var bh = document.createElement('script'), s = document.getElementsByTagName('script')[0];
bh.type = 'text/javascript';
bh.src = '//www.googleadservices.com/pagead/conversion.js';
s.parentNode.insertBefore(bh, s);
}
</script>
<noscript>
<div style="display:inline;">
</div>
</noscript>
the better solutions would be if your templating-language e.g <%= %> supports if else conditions
check your browser console for errors
Related
So I tried this. I put the required code in the head:
<script type="text/javascript">
var callback = function(formatted_number, mobile_number) {
var e = document.getElementById("number");
e.innerHTML = "";
e.appendChild(document.createTextNode(formatted_number));
};
</script>
And then instead of using:
<body onload="_googWcmGet(callback, '0800-123-4567')">
<span id="number">0800-123-4567</span> </body>
I'm using:
<body> <span id="number">0800-123-4567</span> <script type="text/javascript">
window.onload = _googWcmGet(callback, '0800-123-4567'); </script> </body>
It errors out with _googWcmGet being undefined. Think this is due to my lack of knowledge of JS/DOM stuff and that I need to cal _googWcmGet outside the body tag some other way?
I am using prestashop 1.6 . I want to add google ads in top of the header and bottom in footer. I tried many ways but all are not succeed. Please how can i add script in my prestashop website?
Thanks in advance.
You need to find header.tpl file:
https://github.com/PrestaShop/PrestaShop/blob/develop/themes/default-bootstrap/header.tpl
<head>
{$HOOK_HEADER}
<link rel="stylesheet" href="http{if Tools::usingSecureMode()}s{/if}://fonts.googleapis.com/css?family=Open+Sans:300,600&subset=latin,latin-ext" type="text/css" media="all" />
<!--AdWords Code-->
</head>
Remember to disable CCC options for JS (especially moving JavaScript to the end):
Anything within {literal}{/literal} tags is not interpreted, but displayed as-is
{literal}
<script type="text/javascript">
// ...
</script>
{/literal}
{ldelim} and {rdelim} are used for escaping template delimiters, by default { and }:
<script type="text/javascript">
function foo() {ldelim}
// ...
{rdelim}
</script>
gives:
<script type="text/javascript">
function foo() {
// ...
}
</script>
If you still have a problem you may try to override Media Class:
https://gist.github.com/hereswhatidid/8c8edef106ee95138b03
<p>Some HTML goes here</p>
<script type="text/javascript" data-keepinline="true">
// this script will remain here when rendered
alert( "hello!" );
</script>
<script type="text/javascript">
// this script will be forced to the bottom of the page
alert( "hello again!" );
</script>
Media.php:
<?php
Class Media extends MediaCore
{
public static function deferScript($matches)
{
if (!is_array($matches))
return false;
$inline = '';
if (isset($matches[0]))
$original = trim($matches[0]);
if (isset($matches[1]))
$inline = trim($matches[1]);
/* This is an inline script, add its content to inline scripts stack then remove it from content */
if (!empty($inline) && preg_match('/<\s*script(?!.*data-keepinline)[^>]*>/ims', $original) !== 0 && Media::$inline_script[] = $inline)
return '';
/* This is an external script, if it already belongs to js_files then remove it from content */
preg_match('/src\s*=\s*["\']?([^"\']*)[^>]/ims', $original, $results);
if (isset($results[1]) && (in_array($results[1], Context::getContext()->controller->js_files)
|| in_array($results[1], Media::$inline_script_src)))
return '';
/* return original string because no match was found */
return $original;
}
}
The correct way should be using a module. Also check if the function htmlpurifier is blocking your scripts tags.
A little late, but it is solved by using {literal} //script here {/literal}. It's supposed to be used only if there are curly brackets in your script, but it works.
I want to display google ads at vbulletin forum when the thread not contains any crack or serial so I write this code:
<div id ="d_content"> post goes here...</div>
<div id="adv_content"></div>
<script>
function isBlank(str) {
return (!str || /^\s*$/.test(str));
}
var str = document.getElementById("d_content").innerHTML;
var pattern = /crack|nulled|serial|register|key|pach|patch/gi;
var Len= str.match(pattern);
if( isBlank(Len) ) document.write("adv..");
</script>
but how to put the google ad code instead of "adv.."
this is my try :
<div id ="d_content"> post goes here...</div>
<div id="adv_content"></div>
<script>
function isBlank(str) {
return (!str || /^\s*$/.test(str));
}
var str = document.getElementById("d_content").innerHTML;
var pattern = /crack|nulled|serial|register|key|pach|patch/gi;
var Len= str.match(pattern);
if( isBlank(Len) ) document.write("
<script type="text/javascript"><!--
google_ad_client = "ca-pub-1564912551365218";
/* 728x90, تم إنشاؤها 02/06/11 زهير طه */
google_ad_slot = "7785253845";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
");
</script>
but not worked..
Your code is not going to work because browsers will interpret the first </script> as the end of your script, eve though it is within a text string.
A workaround is to use an escape/unescape sequence:
document.write(unescape("%3Cscript type='text/javascript'%3E[your script here]%3C/script%3E"));
where < is escaped as %3C and > is escaped as %3E.
I have a setup to display something when it detects adblock, and it's displaying code instead of working. I am not that experienced in Javascript to know why.
Okay, in my index file I have
<script type="text/javascript" src="inls/advertisement.js"></script>
Now in advertisement.js I have
document.write('<div id="tester">an advertisement</div>');
And now I have the following in my index after that:
<script type="text/javascript">
if (document.getElementById("tester") != undefined)
{
document.write('<center><script type="text/javascript"><!--google_ad_client = "ca-pub-3846434391631935";/* talknow */google_ad_slot = "6591659297";google_ad_width = 728; google_ad_height = 90; //--> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script></center>');
} else {
document.write('<p class="no">We\'ve detected that you\'re using <strong>AdBlock Plus</strong> or some other adblocking software. Please be aware that this is only contributing to the demise of the site. We need money to operate the site, and almost all of that comes from our online advertising. Its not nice to steal.<!-- end .content --></p>');
}
</script>
but where that code is, it displays as this:
Can anyone help?
I took a look at your actual website's source code. It's different to what you posted:
<script type="text/rocketscript">
if (document.getElementById("tester") != undefined)
{
document.write('<center><script type="text/javascript"><!--google_ad_client = "ca-pub-3846434391631935";/* talknow */google_ad_slot = "6591659297";google_ad_width = 728; google_ad_height = 90; //--> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script></center>'); } else {
document.write('<p class="no">We\'ve detected that you\'re using <strong>AdBlock Plus</strong> or some other adblocking software. Please be aware that this is only contributing to the demise of the site. We need money to operate the site, and almost all of that comes from our online advertising. Its not nice to steal.<!-- end .content --></p>'); }
</script>
Based on Why is wordpress placing "text/rocketscript" instead of "text/javascript" when using wp_register_script()? it seems like you need to change your <script> tag to:
<script data-cfasync="false" type="text/javascript>
Also, document.write is considered "bad practice"; you should probably use DOM manipulation instead, i.e. something like:
var adblock = document.createElement('p');
adblock.className = 'no';
adblock.innerHTML = 'Some text here';
var content = document.getElementById('content');
content.insertBefore(adblock, content.firstChild);
I would like to know how to pass the value of a variable in JavaScript file to a variable in a script function which is written on top of the HTML file.
I wrote it like this:
myjsfile.js:
var abc = "10";
HTML file:
<html>
<head>
<script type="text/javascript">
(function() {
var test = document.createElement('script'); test.type = 'text/javascript'; test.async = true;
test.src = 'testquery.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(test, s);
alert(abc);
})();
</script>
</head>
<body>
</body>
</html>
I am not getting the output. Please help! Thanks in advance.
Basically I am trying to create a jquery plugin just like Google analytics.
The script has to load first, try using onload
test.onload=function(){alert(abc);}
<html>
<head>
<script type="text/javascript" src="testquery.js"></script>
<script type="text/javascript">
alert(abc);
</script>
</head>
<body></body>
</html>
try this:
(function() {
var test = document.createElement('script');
document.getElementsByTagName('head')[0].appendChild(test);
test.type = 'text/javascript';
test.src = 'testquery.js';
test.onload = function () {
alert(abc);
}
})();
well.. the onload has already been told but you should at least switch your way of appending it to the head.