...

View Full Version : how do I get a js file to work from html



Tony Webb
07-04-2012, 08:14 PM
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.

c1lonewolf
07-04-2012, 08:32 PM
Yeah but did you try putting the js link before the actual function calls?



<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>

Tony Webb
07-04-2012, 08:54 PM
Thanks for your reply


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?

c1lonewolf
07-04-2012, 09:10 PM
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.

Tony Webb
07-04-2012, 09:19 PM
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 >



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum