Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry how do I get a js file to work from html

    I don't know any js coding. I've just copied these snipets from other sites and tinkered with them. The code works when embedded into an html

    I created the following code for a js file mobiles.js;

    function detect_size()
    {
    if(screen.width<=650)
    {
    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();
    }
    if(screen.width<=995)
    {
    window.location="http://what-do-christians-believe.co.uk/book.html";
    exit();
    }

    I then used the following html code to get the js file read

    <script src="mobiles.js" type="text/javascript"></script>

    The code doesn't work as a seperate js file or it's not being read by my index.html file. I've only put the script code in the index.html file to test it.

    What do I need to do to get the js code to work from an html file?

    I've tried making the js file into a htm file and that hasn't worked either. I've also tried screaming, shouting and going for a bike ride!

    Thanks.

  • #2
    Regular Coder
    Join Date
    Sep 2002
    Posts
    428
    Thanks
    0
    Thanked 20 Times in 20 Posts
    Yeah but did you try putting the js link before the actual function calls?


    Code:
    <script src="mobiles.js" type="text/javascript"></script>
    
    <script type="text/javascript">
    function detect_size()
    {
    if(screen.width<=650)
    {
    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();
    }
    if(screen.width<=995)
    {
    window.location="http://what-do-christians-believe.co.uk/book.html";
    exit();
    }
    </script>
    NO Limits!! www.dhcreationstation.com
    ------------------------------------------------------------
    Broken items wanted for tinkerin'! PostItNow@BrokenEquipment.com

  • #3
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your reply

    Quote Originally Posted by c1lonewolf View Post
    Yeah but did you try putting the js link before the actual function calls?
    I'm not sure what you mean.

    I had the function call in an html file and the javascript in a separate file called mobile.js

    It seems that the function in my html file isn't working as my site defaults to full size.

    My understanding is that by calling the js file from an html file would make downloading my urls quicker.

    Do I repeat the link in the js file?

  • #4
    Regular Coder
    Join Date
    Sep 2002
    Posts
    428
    Thanks
    0
    Thanked 20 Times in 20 Posts
    Copy the code above and place it in the head of your html document, then run it. PS: the last 2 screen width conditions are the same size but go to 2 different pages.
    NO Limits!! www.dhcreationstation.com
    ------------------------------------------------------------
    Broken items wanted for tinkerin'! PostItNow@BrokenEquipment.com

  • #5
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by c1lonewolf View Post
    Copy the code above and place it in the head of your html document, then run it. PS: the last 2 screen width conditions are the same size but go to 2 different pages.
    Thanks for your reply.

    The code is set up and works in my html file. I want to delete this code and create a js file with the script and call it from the html file. I understand It speeds up the downloading of the website. It's pretty slow on mobiles although I have kept content to a minimum and only use 1 very small jpeg.

    I used the html script link but it seems that it is not working.
    Thanks for pointing out the size error. I'll change the last one to >


  •  

    Tags for this Thread

    Posting Permissions

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