0

I am trying to load an external javascript file if the users viewing the page on ipad but it doesn't matter what i do or where I put this code, it doesn't load the iPad.js whatsoever when I view the page from my iPad 2 and it keeps loading the normal.js file!

Here is the code:

  <script language="javascript">

if (navigator.userAgent.match(/iPad/i) != null){ // may need changing?
  var js = document.createElement('script');
  js.type = "text/javascript";
  js.src = "iPad.js";

  var h = document.getElementById('closeHtml2')[0];
  h.appendChild(js);
}
</script>

And this is how I get the normal.js on my page:

<script src="normal.js" type="text/javascript" charset="utf-8"></script> 

could someone please help me out with this?

user3454730
  • 281
  • 2
  • 4
  • 20
  • 1
    I think you're supposed to use ` – Ilan Mar 26 '14 at 15:47
  • Instead of ` – Ilan Mar 26 '14 at 15:51

1 Answers1

1

There is no getElementsByID, it's getElementById and it's not a nodeList, but a single element

if (navigator.userAgent.match(/iPad/i) != null){ // may need changing?
  var js = document.createElement('script');
  js.type = "text/javascript";
  js.src = "iPad.js";

  var h = document.getElementById('closeHtml2');
  h.appendChild(js);
}
adeneo
  • 312,895
  • 29
  • 395
  • 388
  • sorry that was a typo.. but even if I type getElementById, it doesn't work. – user3454730 Mar 26 '14 at 15:50
  • @user3454730 - Why are you appending a script tag to an element, normally you'd insert it into the head, but if you have an element matching the selector, this works fine. – adeneo Mar 26 '14 at 15:52
  • @user3454730 - Also note that on linux servers the filename is case sensitive. – adeneo Mar 26 '14 at 15:53
  • I do have a div `
    ` and this still doesn't work!
    – user3454730 Mar 26 '14 at 15:57
  • Try `var h = document.head`, and open the console to see if the file returns 404 and wether or not the script tag was inserted etc. – adeneo Mar 26 '14 at 16:00
  • with var head it works ! but i need to place in that Div tag. i don't want to put it in the head. any suggestion? – user3454730 Mar 26 '14 at 16:06
  • Why would you put a script tag inside a DIV, it's not really where you're supposed to put script tags, the body or the in most cases the head is where you'd put them. Anyway, check that the ID matches exactly, and I assume you used the code above, without the `[0]`, as that's invalid with `getElementById` – adeneo Mar 26 '14 at 16:08
  • is there any way to put it at the bottom of the body tags? I need the code to be at the bottom of the page and that div tag is at the bottom of the page. – user3454730 Mar 26 '14 at 16:18
  • `var h = document.body` – adeneo Mar 26 '14 at 16:19
  • that puts it at the top of the body though! I need it to go right at the bottom of the body/page. – user3454730 Mar 26 '14 at 16:23
  • `appendChild` appends, so `document.body.appendChild` places the child as the last element inside the body at that time, so how you're able to prepend elements with `appendChild` is beyond me ? – adeneo Mar 26 '14 at 16:30
  • I accepted this answer as it helped but I just don't know how it helped! lol somehow, what I was trying to do for hours started working by just talking to you.. you magical man you...:D anyway, thanks for your help – user3454730 Mar 26 '14 at 16:54