Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript load src javascript file

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #185628
    marcustisater
    Participant

    I have a script like this;

    <script type=”text/javascript” src=”http://example/address-here.js”&gt;
    </script>

    Usually you would put this between the head but I want to have this in my js file since I got a bit of more code before this loads. Also it’s nicer since I don’t want to have that in my head….

    I want to load the src in javascript my js file.

    How do I make that possible?

    I got a function

         $(function(){
            var counts = 1
               $(document).click(function(){
                   if(counts=2){
                      counts+=1;
                }
                else{
                   // i want to load my script here
              }
          });
       })
    

    hopefully you guys understand

    thanks

    #185849
    jamygolden
    Member

    I’m not really sure I understand you, but your example function wouldn’t work as you are settings counts=2 within your if statement which will always be true.

     jQuery(function($) {
        var counts = 1;
    
        $(document).click(function() {
            counts++;
    
            if (counts === 3) {
               var scriptEl = document.createElement('script');
               scriptEl.src = 'http://example/address-here.js';
               document.body.appendChild(scriptEl);
          }
      });
    })
    

    That will add a script file to the page if a user clicks on the document twice.

Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.