View Full Version : Putting Javascript into Javascript?
PeterJS
12-20-2002, 09:45 AM
Hi :)
I was wondering if its possible to be able to put this.........
<script language="JavaScript" src="http://m1.nedstatbasic.net/basic.js">
</script>
<script language="JavaScript">
<!--
nedstatbasic("ABomAgaOz8xEZjvTRu0ZvDvpvK+A", 0);
// -->
</script>
<noscript>
<img
src="http://m1.nedstatbasic.net/n?id=ABomAgaOz8xEZjvTRu0ZvDvpvK+A"
border="0" nosave width="18" height="18">
</noscript>
into this script where it is bold........
<SCRIPT LANGUAGE="JavaScript">
<!--
if (location.href.indexOf("=register") != -1) {
document.write('INTO HERE');
}
-->
</SCRIPT>
I have tried it but I keep getting errors. its going to be used on the register page of our message board, so that it displays the counter just on the register page. We don't have access to the templates, but we have headers and footers to put html and javascript in. I managed to get an image to be displayed just on the register page, but I can't figure out the above.
Hope someone can help
thanks
Peter :D
it's all about escaping the <script> and </script> tags properly.
the browser just reads through the doc write and if it finds a </script> it assumes the tags enclosing the doc write to be closed
<s\cript> = ok
</s\cript> = ok
you could pretty much escape any letter except the 's' 'r' or 't'
document.write('<s\cript language="JavaS\cript" src="http://m1.nedstatbasic.net/basic.js">\n</s\cript>\n<s\cript language="JavaS\cript"><!--\n nedstatbasic("ABomAgaOz8xEZjvTRu0ZvDvpvK+A", 0);\n// -->\n</s\cript><nos\cript><img
src="http://m1.nedstatbasic.net/n?id=ABomAgaOz8xEZjvTRu0ZvDvpvK+A"
border="0" nosave width="18" height="18">\n</nos\cript>');
document.write('<s\cript language="JavaS\cript" src="http://m1.nedstatbasic.net/basic.js">\n'
+'</s\cript>\n'
+'<s\cript language="JavaS\cript">\n'
+'<!--\n'
+'nedstatbasic("ABomAgaOz8xEZjvTRu0ZvDvpvK+A", 0);\n'
+'// -->\n'
+'</s\cript>\n'
+'<nos\cript>\n'
+'<img src="http://m1.nedstatbasic.net/n?id=ABomAgaOz8xEZjvTRu0ZvDvpvK+A"'
+' border="0" nosave width="18" height="18">\n'
+'</nos\cript>');
looks neater :)
PeterJS
12-20-2002, 10:14 AM
thanks very much, i'll give that a try ;)
Peter :)
PeterJS
12-20-2002, 10:24 AM
Hi,
I tried it, but I keep getting a error message....."Object Expected"
Peter :)
PeterJS
12-20-2002, 08:01 PM
I still can't sort it out.
I keep getting an object expected error. Is there something wrong in the script?
hope someone can help me
thanks
Peter :)
where is function
nedstatbasic("ABomAgaOz8xEZjvTRu0ZvDvpvK+A", 0)
if it is in
<script language='JavaScript' src='http://m1.nedstatbasic.net/basic.js'></script>
this needs to be placed after the function that calls it
joeframbach
12-20-2002, 08:38 PM
hmm... is nedstatbasic() in the js file?
joeframbach
12-20-2002, 08:39 PM
bah! you beat me!:( :thumbsup:
joeframbach
12-20-2002, 08:44 PM
i blame it on my slooow connection (56k)
a useful hint for checking the textual output of a document write is to use a window.open call and write the stuff there.
a = window.open();
a.document.write('.............');
then just view source in the opened window.
If you had done that with the coding I posted above, you'd have seen it works perfectly and the object expected error must therefore be due to something else.
ca_redwards
12-27-2002, 10:12 PM
In my HTML() bookmarklet library (http://www.angelfire.com/ca/redwards/html__.calendar.html), I was challenged with dynamically creating JavaScript from other JavaScript.
Here's what I came up with: document.write('alert()'.SCRIPT(LANGUAGE('javascript')+TYPE('text/javascript')));
Note that the .SCRIPT() function doesn't include a literal /SCRIPT to boff the browser's HTML parsing. It dynamically creates it for the second pass.
Works great!
brothercake
12-27-2002, 10:19 PM
Originally posted by ca_redwards
I was challenged with dynamically creating JavaScript from other JavaScript.
No disrespect, but you haven't dynamically created a script, you've just written the tag onto the page using javascript; procedurally, that's no different to using normal HTML.
You can create script includes dynamically, using the create element method:
var newScript = document.createElement("script");
newScript.type = "text/javascript";
newScript.src = "script.js";
document.body.appendChild(newScript);
ca_redwards
12-30-2002, 08:36 PM
Your method using document.createElement is unquestionably dynamic. And while my method may seem no different that coding a SCRIPT tag in pure HTML, it is not hardcoded into the HTML... it originates from my JavaScript's successful execution. I suppose that exactly when it gets parsed does differ from your method.
I expect that yours might run a bit faster, but that mine is more cross-browser (older browsers).
Six of one, half-dozen of the other.
Originally posted by brothercake
No disrespect, but you haven't dynamically created a script, you've just written the tag onto the page using javascript; procedurally, that's no different to using normal HTML.
You can create script includes dynamically, using the create element method:
var newScript = document.createElement("script");
newScript.type = "text/javascript";
newScript.src = "script.js";
document.body.appendChild(newScript);
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.