include js timeout - help needed :)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stormsender
    New Member
    • Feb 2007
    • 5

    include js timeout - help needed :)

    Hi there, I'm a noob to javascript, so was hopping to find some help here...

    I believe that what I need is simple, still haven't found any solution.

    I have a website serving ads, and links from several systems, and they always come in a format like:

    <script language=javasc ript src="http://something.com/script.php?id=2 6758765"></script

    This is fine, however I would like to establish a time-out for this, like for instance, if something.com/script.php?id=2 6758765 is not available in 2 seconds, the page would still load instead of trying to load that javascript forever.

    What can I do?
    Please post some example code. I'm a noob... sorry.
  • wgale025
    New Member
    • Feb 2007
    • 23

    #2
    有点明白,但是英文不好,不知道 怎么帮你!
    不知道你是不是用documen t.write()或者docu ment.writeln(). 这两者必须在页面加载前用,要不 然你的页面将会被打印出来的值覆 盖!

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Originally posted by stormsender
      Hi there, I'm a noob to javascript, so was hopping to find some help here...

      I believe that what I need is simple, still haven't found any solution.

      I have a website serving ads, and links from several systems, and they always come in a format like:

      <script language=javasc ript src="http://something.com/script.php?id=2 6758765"></script

      This is fine, however I would like to establish a time-out for this, like for instance, if something.com/script.php?id=2 6758765 is not available in 2 seconds, the page would still load instead of trying to load that javascript forever.

      What can I do?
      Please post some example code. I'm a noob... sorry.
      Welcome to The Scripts.

      Is the PHP generating javascript code.

      If it's static, why not include the javascript in your PHP page?

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Originally posted by wgale025
        有点明白,但是英文不好,不知道 怎么帮你!
        不知道你是不是用documen t.write()或者docu ment.writeln(). 这两者必须在页面加载前用,要不 然你的页面将会被打印出来的值覆 盖!
        Translation please!!

        Comment

        • stormsender
          New Member
          • Feb 2007
          • 5

          #5
          Thanks for your reply.
          That little piece of code is the same but the content it generates is random, just like adsense, or other networks.

          My problem is, that if you put that piece of code in the middle of a page it will work fine, however if the ad server is down when I try to load my page there is a problem...
          My browser starts to read the page, but will hangs in the middle of that page trying to read that javascript forever.
          If there is no connectivity to that ad server, it will take forever to give up, and load the rest of content in my page.

          If I put that include on top of my pages, it first tries to load that javascript... so it will not open my webpage before that includes. If the ad server brings a 404, the page loads fine, but if te server is down, no error message will be given, so my website and my browser will try to open that includes forever, before everything else.

          What I want is to limit that wait for that includes.

          Something like:

          start measuring time,
          if loading time > 2 seconds = die(output text : ADS not available.)
          else if
          loading time < 2 seconds
          include file.js

          Is it possible to check for a 200 OK response within 2 seconds in javascript?

          thanks

          Comment

          • stormsender
            New Member
            • Feb 2007
            • 5

            #6
            Ha!, now I find out the solution :)

            I have been reading some javascript books, and I after all I get this to work.

            replace your ad with this example:

            Go to the exact place you want your ads displayed, like on the sidebar, or the middle of the page and replace:

            Code:
            <script language="javascript" type="text/javascript" src="http://ads-or-whatever-it-is.com/script.php?id=24324242"></script>
            with:

            Code:
            <div id="name_of_your_add"><p>Loading Partners or whatever you like here...</p></div>
            then on the very bottom end of the page, add:
            (should be your footer if you're using a script portal, blog or whatever).

            Code:
            <script language="javascript"><!--
            var old_document_write = document.write;
            var name_of_your_add_data = "";
            document.write = function(s) {
            name_of_your_add_data += s;
            }
            // --></script>
            <script language="javascript" type="text/javascript" src="http://ads-or-whatever-it-is.com/script.php?id=24324242"></script>
            <script language="javascript"><!--
            document.write = old_document_write;
            document.getElementById("name_of_your_add").innerHTML = name_of_your_add_data;
            // --></script>
            and you're done.

            This will allow all page to load fine, and in the end, if network is not available, or ads are not available because they are slow, they will not interfere with your page load.

            Check my website at http://hi7.co.uk

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              A simple solution in the end. The most elegant solution would be to detect the loading of the document and timeout after 2 seconds, but if it's not possible, then why not? I am unsure why your code replaces document.write though. Is that really necessary?

              Comment

              • stormsender
                New Member
                • Feb 2007
                • 5

                #8
                Originally posted by acoder
                A simple solution in the end. The most elegant solution would be to detect the loading of the document and timeout after 2 seconds, but if it's not possible, then why not? I am unsure why your code replaces document.write though. Is that really necessary?
                And how to detect the loading of the document and timeout after 2 seconds?
                Can you post an example?

                I have used this, because I don't know how to do what you've suggested...

                Imagine that file.js , takes 30 seconds to load due to network problems, or server load.
                I have done this because i don't want to wait 30 seconds, in order to load the rest of the page.
                But how to detect the loading of the document and timeout after 2 seconds?

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  I said it's a more elegant solution, not that it's possible. See this thread. It might help. What you need effectively is an event handler that can detect chnages in the loading state.

                  What I suggest you can do is get the time using Date(). Then make a request to load the document and check after two seconds. If not loaded, then abort. To check if it's loaded, you could check for an object, e.g. if you have an object with id "ad_id", you could try:
                  Code:
                  if (document.getElementById("ad_id")) {...
                  I haven't tried this so I don't know if it will work.

                  Comment

                  • stormsender
                    New Member
                    • Feb 2007
                    • 5

                    #10
                    thanks anyway.

                    cheers

                    Comment

                    • wgale025
                      New Member
                      • Feb 2007
                      • 23

                      #11
                      Originally posted by acoder
                      Translation please!!
                      Sorry!
                      I'm English is poor!

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        Originally posted by wgale025
                        Sorry!
                        I'm English is poor!
                        It doesn't matter... most people can understand even if there are quite a few grammatical mistakes. In fact, people born in the UK/US/English speaking countries make so many mistakes too!

                        But, with Mandarin (which I assume it is), most users here have no chance of understanding one letter never mind a word of it.

                        btw, it should be "My English is poor", but no worries, I know how hard it is to communicate in a second (or even third) language.

                        Comment

                        • acoder
                          Recognized Expert MVP
                          • Nov 2006
                          • 16032

                          #13
                          Originally posted by stormsender
                          thanks anyway.

                          cheers
                          There's one other possibility if you're up for it. What you can do is load the page without the ads. Then use Ajax to get the ads. To do this, have a page on your website that loads the ads from the different servers, then make a request to that page. Then you don't have to worry about how long it takes because it is asynchronous.

                          Comment

                          Working...