dpacmittal Posted May 11, 2009 Share Posted May 11, 2009 Is it possible to link javascript dynamically. I mean, like this: <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. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 11, 2009 Share Posted May 11, 2009 Sort of. <script type="text/javascript> var source= location.href+"abc.js"; document.write('<script type="text/javascript" src="' + source + '"></script>'); </script> Quote Link to comment Share on other sites More sharing options...
dpacmittal Posted May 11, 2009 Author Share Posted May 11, 2009 Thanks for the quick reply. But it didn't work. It just outputs '); Quote Link to comment Share on other sites More sharing options...
dpacmittal Posted May 11, 2009 Author Share Posted May 11, 2009 I think it isn't working because we are loading a script inside script tags. Any workaround for that? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 11, 2009 Share Posted May 11, 2009 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 Link to comment Share on other sites More sharing options...
dpacmittal Posted May 11, 2009 Author Share Posted May 11, 2009 <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. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 11, 2009 Share Posted May 11, 2009 Sometimes browsers are a pain. Try this - <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 Link to comment Share on other sites More sharing options...
dpacmittal Posted May 11, 2009 Author Share Posted May 11, 2009 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? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 11, 2009 Share Posted May 11, 2009 Well that's within quotes so you don't need to escape a string. :-\ Quote Link to comment Share on other sites More sharing options...
dpacmittal Posted May 11, 2009 Author Share Posted May 11, 2009 Ahh.. I see Thanks anyways. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.