www.webdeveloper.com
Results 1 to 3 of 3

Thread: Unable to break a loop between javascript and html

  1. #1
    Join Date
    Jul 2012
    Location
    Vantaa, Finland
    Posts
    125

    Unable to break a loop between javascript and html

    I have created a javascript file which is referenced from an html file. The script is looping and I am unable to break out of it.

    What I want to do is to redirect files to different directories for different screen sizes. (I am working on building a responsive site).

    The javascript file is;
    Code:
    if(screen.width<=499)
     {
      window.location="http://what-do-christians-believe.co.uk/mob_phone/";
      exit();
      }
    if(screen.width<=995)
    {
     window.location="http://what-do-christians-believe.co.uk/mob_devices/";
    exit();
    }
    else
    {
     window.location="http://what-do-christians-believe.co.uk/";
    exit();
    }
    I have tried adding index.html and window.location="http://what-do-christians-believe.co.uk/index.html"
    ;

    The link from the html file is; <script src="device.js"></script>

    The javascript code works fine in small devices.

    I think the problem is that the javascript code is read. The correct html file is opened and then the javascript is reread and the program loops.

    How can I stop the looping?

  2. #2
    Join Date
    Nov 2006
    Location
    Oakland
    Posts
    500
    Make sure you do not having the JS above in the default page:
    window.location="http://what-do-christians-believe.co.uk/
    otherwise the same page will be reloaded over and over.
    You can also modify your script as follows:

    if(screen.width<=499)
    {
    window.location="http://what-do-christians-believe.co.uk/mob_phone/";
    }
    else if(screen.width<=995)
    {
    window.location="http://what-do-christians-believe.co.uk/mob_devices/";
    }
    else if(!window.location.pathname && window.location.pathname!="/")
    {
    window.location="http://what-do-christians-believe.co.uk/";
    }

  3. #3
    Join Date
    Jul 2012
    Location
    Vantaa, Finland
    Posts
    125
    Thanks for your reply. it works a treat!

    I will now mess about with making my site responsive, but that can wait until the weekend!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles