how to link to external javascript which link to another external javascript???

Discussion in 'JavaScript' started by shuman202, Jul 21, 2012.

  1. #1
    hello guys,
    i have a javascript code which look like that
    
     <script type="text/javascript" src="http://paypopup.com/code.js?id=3133"></script> 
    
    Code (markup):
    its a code for popup ads
    i have to add it in every webpage...
    i'm trying to add this code in a separate which i call it myscript.js
    and i want to link to it from all the site pages using the following code

    
    <script type="text/javascript" src="myscript.js"></script>
    
    Code (markup):
    is that possible to happen?????
     
    shuman202, Jul 21, 2012 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #2
    There are two ways of handling it... the first is, well... sloppy and old -- using document.write to add it in the page during load... Really that method belongs in the past.

    The modern way is to manipulate the DOM.

    
    var newElement=document.createElement('script');
    newElement.setAttribute('type','text/javascript');
    newElement.setAttribute('src','pathto/yourscript.js');
    var docHead=document.getElementsByTagName('head')[0];
    docHead.appendChild(newElement);
    
    Code (markup):
    The same method can be used to load CSS from javascript-- createElement('link'), and be sure to set the proper TYPE, REL and MEDIA values.

    Note that functions and code from that script may not be available until the script you call it in ends/breaks for a bit.
     
    deathshadow, Jul 21, 2012 IP
  3. shuman202

    shuman202 Well-Known Member

    Messages:
    638
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    145
    Digital Goods:
    1
    #3
    it didn't work
    anyway thank you for your help
     
    shuman202, Jul 22, 2012 IP