Author Topic: Linking javascript dynamically  (Read 939 times)

0 Members and 1 Guest are viewing this topic.

Offline dpacmittalTopic starter

  • Enthusiast
  • Posts: 218
  • Gender: Male
    • View Profile
    • Absolutely Tech!
Linking javascript dynamically
« on: May 11, 2009, 12:01:18 PM »
Is it possible to link javascript dynamically. I mean, like this:

Code: [Select]
<script type="text/javascript>
var source= location.href+"abc.js";
</script>
<script src=source></script>


I tried the above code but it didn't work. I tried using eval() but it still didn't work. Anyway we can make that work? Help me guys.. this is important.

Offline Ken2k7

  • Freak!
  • Posts: 5,174
    • View Profile
Re: Linking javascript dynamically
« Reply #1 on: May 11, 2009, 12:03:28 PM »
Sort of.

Code: [Select]
<script type="text/javascript>
var source= location.href+"abc.js";

document.write('<script type="text/javascript" src="' + source + '"></script>');
</script>
Quote from: Slaytanist
A programmer who shys away from elegant tricks will never be more than competent at best. Ego and a desire to attempt the impossible are traits of most great coders.

Offline dpacmittalTopic starter

  • Enthusiast
  • Posts: 218
  • Gender: Male
    • View Profile
    • Absolutely Tech!
Re: Linking javascript dynamically
« Reply #2 on: May 11, 2009, 12:09:13 PM »
Thanks for the quick reply. But it didn't work. :(

It just outputs ');

Offline dpacmittalTopic starter

  • Enthusiast
  • Posts: 218
  • Gender: Male
    • View Profile
    • Absolutely Tech!
Re: Linking javascript dynamically
« Reply #3 on: May 11, 2009, 12:16:36 PM »
I think it isn't working because we are loading a script inside script tags. Any workaround for that?

Offline Ken2k7

  • Freak!
  • Posts: 5,174
    • View Profile
Re: Linking javascript dynamically
« Reply #4 on: May 11, 2009, 12:17:39 PM »
No, it does work. I used that numerous times writing JavaScript mods for remote hosting forums.

I know it's small, but can you post what you have?
Quote from: Slaytanist
A programmer who shys away from elegant tricks will never be more than competent at best. Ego and a desire to attempt the impossible are traits of most great coders.

Offline dpacmittalTopic starter

  • Enthusiast
  • Posts: 218
  • Gender: Male
    • View Profile
    • Absolutely Tech!
Re: Linking javascript dynamically
« Reply #5 on: May 11, 2009, 12:23:49 PM »
Code: [Select]
<script type="text/javascript">
var source="http://localhost/unique_links/link.php?domain="+location.href;
document.write('<script type="text/javascript" src="' + source + '"></script>');
</script>

This is my code. It doesn't work.
« Last Edit: May 11, 2009, 12:24:27 PM by dpacmittal »

Offline Ken2k7

  • Freak!
  • Posts: 5,174
    • View Profile
Re: Linking javascript dynamically
« Reply #6 on: May 11, 2009, 12:26:46 PM »
Sometimes browsers are a pain. Try this -

Code: [Select]
<script type="text/javascript">
var source="http://localhost/unique_links/link.php?domain="+location.href;
document.write('<script type="text/javascript" src="' + source + '"><\/script>');
</script>
Quote from: Slaytanist
A programmer who shys away from elegant tricks will never be more than competent at best. Ego and a desire to attempt the impossible are traits of most great coders.

Offline dpacmittalTopic starter

  • Enthusiast
  • Posts: 218
  • Gender: Male
    • View Profile
    • Absolutely Tech!
Re: Linking javascript dynamically
« Reply #7 on: May 11, 2009, 12:32:05 PM »
Wow.. that worked.. Thanks, buddy.

You are a genius.

One thing. Why did that slash created a problem but not the slash in text/javascript?

Offline Ken2k7

  • Freak!
  • Posts: 5,174
    • View Profile
Re: Linking javascript dynamically
« Reply #8 on: May 11, 2009, 12:38:08 PM »
Well that's within quotes so you don't need to escape a string.  :-\
Quote from: Slaytanist
A programmer who shys away from elegant tricks will never be more than competent at best. Ego and a desire to attempt the impossible are traits of most great coders.

Offline dpacmittalTopic starter

  • Enthusiast
  • Posts: 218
  • Gender: Male
    • View Profile
    • Absolutely Tech!
Re: Linking javascript dynamically
« Reply #9 on: May 11, 2009, 12:42:58 PM »
Ahh.. I see
Thanks anyways.