469,648 Members | 1,504 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,648 developers. It's quick & easy.

Question regarding how to dynamically change external javascript source information

Hi,

Lets get it out there... I'm a newbie to javascript.

I'm trying to build a string for the external source file destination
and be able to change the keywords in the string based on what the
user presses/inputs. So here is what I have so far

<script>

.... some javascript to get the keyword arguments out of the URL ...

//build the src destination w/ the keywords
var $myURL;
$myURL = "http://www.foo.com/?keyword=";
$myURL += qsParm['keyword'];

</script>

..
..
some HTML for the page
..
..

<script src=" XXX "></script>

How do I get the XXX to be my variable that I built up at the top of
my page? I feel like I've tried everything... If I printout the
variable to the page - it is correct so its there, just dont know how
to pass it to src.

Please help

Thanks!
Vince

Feb 9 '07 #1
4 2773
vf******@gmail.com scribed:
>Hi,

Lets get it out there... I'm a newbie to javascript.

I'm trying to build a string for the external source file destination
and be able to change the keywords in the string based on what the
user presses/inputs. So here is what I have so far

<script>

... some javascript to get the keyword arguments out of the URL ...

//build the src destination w/ the keywords
var $myURL;
$myURL = "http://www.foo.com/?keyword=";
$myURL += qsParm['keyword'];

</script>

.
.
some HTML for the page
.
.

<script src=" XXX "></script>

How do I get the XXX to be my variable that I built up at the top of
my page? I feel like I've tried everything... If I printout the
variable to the page - it is correct so its there, just dont know how
to pass it to src.

Please help
Once you've accomplished what you want, aren't you going to have to refresh
the page to load the new script? In refreshing the page, aren't you back to
square 1?
--
Ed Jay (remove 'M' to respond by email)
Feb 9 '07 #2
On Feb 9, 9:57 am, vfpug...@gmail.com wrote:
Hi,

Lets get it out there... I'm a newbie to javascript.

I'm trying to build a string for the external source file destination
and be able to change the keywords in the string based on what the
user presses/inputs. So here is what I have so far
[...]
How do I get the XXX to be my variable that I built up at the top of
my page? I feel like I've tried everything... If I printout the
variable to the page - it is correct so its there, just dont know how
to pass it to src.
A couple of recent threads that might help:

"Dynamic Script Insertion & caching"
<URL: http://groups.google.com.au/group/comp.lang.javascript/
browse_frm/thread/8b6d226964e7ffc9/40c7b03d03a4af28?lnk=gst&q=dynamic
+script+insertion&rnum=1#40c7b03d03a4af28 >

"createTextNode and IE7"
<URL: http://groups.google.com.au/group/comp.lang.javascript/
browse_frm/thread/7e23f42490c301de/32362b99328a2adb?lnk=gst&q=dynamic
+script+insertion&rnum=5#32362b99328a2adb >

Search the archives for "dynamic script insert"
--
Rob

Feb 9 '07 #3
vf******@gmail.com wrote:
<script src=" XXX "></script>

How do I get the XXX to be my variable that I built up at the top of
my page? I feel like I've tried everything... If I printout the
variable to the page - it is correct so its there, just dont know how
to pass it to src.
<script>
var headID = document.getElementsByTagName("head")[0];
var newScript = document.createElement('script');
newScript.type = 'text/javascript';
newScript.src = XXX;
headID.appendChild(newScript);
</script>

The moment headID.appendChild is executed the source file you specified
( XXX ) will attempt to be loaded, the javascript file compiled and then
executed.

That's pretty much all there is to it. If you'd like to see a few nifty
things that can be done with this you can check out an article I wrote
on the subject (warning self promoting link incomming)
http://www.hunlock.com/blogs/Howto_D...script_And_CSS

Good luck!
--
http://www.hunlock.com -- Musings in Javascript, CSS.
$FA
Feb 9 '07 #4
RobG said the following on 2/8/2007 9:07 PM:
On Feb 9, 9:57 am, vfpug...@gmail.com wrote:
>Hi,

Lets get it out there... I'm a newbie to javascript.

I'm trying to build a string for the external source file destination
and be able to change the keywords in the string based on what the
user presses/inputs. So here is what I have so far
[...]
>How do I get the XXX to be my variable that I built up at the top of
my page? I feel like I've tried everything... If I printout the
variable to the page - it is correct so its there, just dont know how
to pass it to src.

A couple of recent threads that might help:

"Dynamic Script Insertion & caching"
<URL: http://groups.google.com.au/group/comp.lang.javascript/
browse_frm/thread/8b6d226964e7ffc9/40c7b03d03a4af28?lnk=gst&q=dynamic
+script+insertion&rnum=1#40c7b03d03a4af28 >

"createTextNode and IE7"
<URL: http://groups.google.com.au/group/comp.lang.javascript/
browse_frm/thread/7e23f42490c301de/32362b99328a2adb?lnk=gst&q=dynamic
+script+insertion&rnum=5#32362b99328a2adb >

Search the archives for "dynamic script insert"

document.write('<script src="' + XXX + '"><\/script>')

Hmmm :-)

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 9 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Chad Lupkes | last post: by
38 posts views Thread by Shaun McKinnon | last post: by
11 posts views Thread by NS | last post: by
5 posts views Thread by lixiaoyao | last post: by
4 posts views Thread by cjm | last post: by
10 posts views Thread by jojobar | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.