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. Link to comment https://forums.phpfreaks.com/topic/157700-linking-javascript-dynamically/ 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> Link to comment https://forums.phpfreaks.com/topic/157700-linking-javascript-dynamically/#findComment-831653 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 '); Link to comment https://forums.phpfreaks.com/topic/157700-linking-javascript-dynamically/#findComment-831659 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? Link to comment https://forums.phpfreaks.com/topic/157700-linking-javascript-dynamically/#findComment-831666 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? Link to comment https://forums.phpfreaks.com/topic/157700-linking-javascript-dynamically/#findComment-831668 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. Link to comment https://forums.phpfreaks.com/topic/157700-linking-javascript-dynamically/#findComment-831677 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> Link to comment https://forums.phpfreaks.com/topic/157700-linking-javascript-dynamically/#findComment-831680 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? Link to comment https://forums.phpfreaks.com/topic/157700-linking-javascript-dynamically/#findComment-831684 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. :-\ Link to comment https://forums.phpfreaks.com/topic/157700-linking-javascript-dynamically/#findComment-831690 Share on other sites More sharing options...
dpacmittal Posted May 11, 2009 Author Share Posted May 11, 2009 Ahh.. I see Thanks anyways. Link to comment https://forums.phpfreaks.com/topic/157700-linking-javascript-dynamically/#findComment-831692 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.