I have the following problem:
<script type="text/javascript">
alert("1. ČĆŽŠĐčćžšđ");
</script>
<script type="text/javascript" src="Tst.js"></script>
<script type="text/javascript">
var pScript = document.createElement("script");
pScript.type = "text/javascript";
pScript.src = "Tst.js";
pScript.charset = "windows-1250";
$("body").append(pScript);
</script>
(These are Croatian characters.)
Contents of Tst.js is:
alert("2. ČĆŽŠĐčćžšđ");
Output of this script in FireFox (and Safari, so I've concluded that this is not the problem with the browser, but my code):
1. ČĆŽŠĐčćžšđ
2. ČĆŽŠĐčćžšđ
2. �Ǝ���枚�
Charset on the main page that is calling this code is Windows-1250.
I don't understand why when I call Tst.js statically (by <script src="Tst.js" type="text/javascript"></scipt>) the characters are shown normal, but when I dynamically include Tst.js the characters go bannanas...
And unfortunately I can't port all my code to UTF-8.
Any advice?
2nd update: Specifying the encoding in the content-type header of the JavaScript file did the trick - for whatever reason!
Update: You are setting the character set after loading the script.
Try
<script type="text/javascript">
var pScript = document.createElement("script");
pScript.type = "text/javascript";
pScript.charset = "windows-1250";
pScript.src = "Tst.js";
$("body").append(pScript);
</script>
Related
I have been trying to add this to my page and I am getting this error in Firefox under the JS errors section.
An unsupported character encoding was declared for the HTML document using a meta tag. The declaration was ignored.
My code is below
<script type="text/javascript"><!--
google_ad_client = "ca-pub-12345678987654321";
/* OAS home */
google_ad_slot = "123456789";
google_ad_width = 120;
google_ad_height = 240;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
The error is related to the section of your document, you're using a bad character encoding I guess, ( see http://www.w3schools.com/tags/att_meta_charset.asp )
Consider adding head section code for further help.
I am trying to grab information from the page url to set the src for a data file.
So, say page url is: page.html?x=data_file_3
(The ideas is I could change the url to access other data files: data_file_4, etc.)
I grab the "data_file_3" part of the url and put it in a variable:
(the code I use for this works fine -- so result is)
folder = "/data_file_3/content.js" -- the content of this file is just an array
Then I try this:
<script id="url" type="text/javascript"></script>
<script language="javascript">
...
var u = document.getElementById('url');
u.src = folder;
...
</script>
But this doesn't work (the array data does not show up on the page). I put this code right where I used to hard code:
<script type="text/javascript" src="/data_file_3/content.js"></script>
The hard-coded version works. Any ideas about how I can do this?
Sounds like you are trying to create script tags dynamically.
var scr = document.createElement('script');
scr.src = 'script_path';
document.getElementsByTagName('head')[0].appendChild(scr);
You can wrap this in a function where 'script_path' is whatever you're path you're passing in.
Note also that 'text/javascript' is not required. All browsers understand that its javascript.
I would like to place a javascript (adsense) code inside the post (not above or after the post). It will be a HTML page.
Is there any way i can put my adsense code in external Js file and i will use one function to display it.
adsense code looks something like
<script type="text/javascript"><!--
google_ad_client = "pub-xxxxxxxxxxxxxxxx";
google_ad_host = "pub-xxxxxxxxxxxxxxxx";
google_ad_slot = "xxxxxxxxxx";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
So if i call a function CallMe() which will start showing ad wherever i have used the function. In future if i would like to replace ad code with another code then i dont want to go to each post and replace it. I will just replace the adcode from js file.
I am a newbie and have just started learning JavaScript so i am really not aware if it can be done or not.
Any suggestion ?
Create file called AdSense.js with the following code:
google_ad_client = "pub-xxxxxxxxxxxxxxxx";
google_ad_host = "pub-xxxxxxxxxxxxxxxx";
google_ad_slot = "xxxxxxxxxx";
google_ad_width = 336;
google_ad_height = 280;
function ApplyAdSense() {
var oScript = document.createElement("script");
oScript.type = "text/javascript";
oScript.src = "http://pagead2.googlesyndication.com/pagead/show_ads.js";
document.getElementsByTagName("head")[0].appendChild(oScript);
}
Now whenever you want adsense in your code, first include the file:
<script type="text/javascript" src="AdSense.js"></script>
Then call the function:
<script type="text/javascript">
ApplyAdSense();
</script>
This way, until you call the function nothing happens.. and you can also comment the code inside the function to disable adsense throughout all your site.
Wherever you want the ad to show up, place this code (assuming you have a function called CallMe).
<some html>
<script type="text/javascript">CallMe();</script>
</some html>
If your concern is about the page loading time, Adsense released the asynchronous version of their Adsense code. Please see https://support.google.com/adsense/answer/3221666?hl=en
I'm working on a single page app that will probably fire more than 1 conversion per pageload.
I need to fire the google conversion snippet, so I assumed that I could throw it in at run time. The snippet looks something like this:
<script type="text/javascript">
/* <![CDATA[ */
var google_conversion_id = XXXXXXXX;
var google_conversion_language = "en";
var google_conversion_format = "2";
var google_conversion_color = "ffffff";
var google_conversion_label = "XXXXXXXXXXXXXXX";
var google_conversion_value = 25.00;
/* ]]> */
</script>
<script type="text/javascript" src="http://www.googleadservices.com/pagead/conversion.js">
</script>
I have been using javascript to insert the snippet when a conversion has fired. I'm doing it like this:
function(id,url,content){
// add script
var script = document.createElement("script");
script.type = "text/javascript";
if(url) script.src = url;
if(content) script.text = content;
var bucket = document.getElementById(id);
bucket.appendChild(script);
debugger;
}
This works in all the browsers I've tried it in, except safari.
In safari, when the 2nd script tag is added, the entire body tag's content is replaced with a google iFrame. The whole dom is nuked in fact. The head's content is wiped out as well.
What the hell is happening in that google script, and how do I insert this without everything blowing up?!?
Update:
Looks like for some reason safari didn't like the way I was adding the script. To fix this, I added bucket.innerHTML = '' below the debugger line and it worked great in Safari. Unfortunately, that caused FF 3.6 to do what safari was previously doing and nuke the DOM.
To make it even more complicated, it seems AdWords was rejecting these conversions or something, they don't show up on the reporting end when injected to the page.
My current approach is to use htaccess and a little string parsing to generate a page with nothing but the conversion snippet on it, and iFrame it in. I'll report back on that.
Perhaps it might be easier to insert the Google Ads via an iFrame?
Such as like this:
<iframe src="/documents/adsense.htm" width="728px" height="90px" frameborder="0" marginwidth ="0px" marginheight="0px" scrolling="no"></iframe>
iFrame contains:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Google Adsense</title>
<base target="_parent">
</head>
<body>
<!--insert Google Adsense Code Here-->
</body>
</html>
Google will still know and track the parent page.
The below piece of code was working in IE6 & IE7 and almost all versions of FF. It just don't work in IE8. It doesn't work in the sense once I added the script tag in to HTML->HEAD element I don't see the script being loaded in the browser(the alerts in the script doesn't show up). I see the tags have been inserted in the HTML-HEAD though.
var head = document getElementsByTagName('head')[0];
// Check if the script is already loaded.
if (head ){
var script = document.createElement('script');
script.type = 'text/javascript';
script.language = 'JavaScript';
script.src = '/Tolven/scripts/' + jsFileName;
head.appendChild(script);
}
Does anybody have this issue? Or any clues to resolve this?
If this script is in <head> tag than head does not exists when this script is parsed and executed. So, of cource if (head) is false.
Your are using JS framework -- so feel free to use it's tools. And also do not forget to include Your framework, before using it.
<!-- if your are using mootools -->
<script type="text/javascript" src="mootools.js"></script>
<script type="text/javascript">
window.addEvent('domready', function() {
// Your code...
});
</script>
<!-- if your are using prototype -->
<script type="text/javascript" src="prototype.js"></script>
<script type="text/javascript">
document.observe("dom:loaded", function() {
// Your code...
});
</script>
Consider using a library like RequireJS or LABjs that do the job of including scripts at runtime really well.
var head = document getElementsByTagName('head')[0];
should be
var head = document.getElementsByTagName('head')[0];
Script seems to work after this modification.
This is was actually working. There is was an error(it only happens in IE8) in one of the scripts that's inserted at runtime. Eventually it's not executing alerts in the pages loaded next. Thanks for your answers though.