0

I have seen a lot of websites about replace in JavaScript, I am new to JavaScript, I appreciate some help?
This JavaScript code didn't work when I tried it in JSFiddle:

window.onload = function() {
  if (navigator.userAgent.indexOf('iPhone|iPod|iPad') != -1) {
    $("a[href$='iPhone']").attr("href", function() {
      return this.href.replace("iPhone", "IIIphone");
    });
  } else {
    $("a[href$='iPhone']").attr("href", function() {
        return this.href.replace("iPhone", "Android");
      }
    }
  }
}

and my HTML is

<a href="http://www.mypage.com?iPhone-type=iPhone">The 1 link</a>
<a href="http://www.mypage2.com?iPhone-type=iPhone">The 2 link</a>
MD. Khairul Basar
  • 4,976
  • 14
  • 41
  • 59
d00nn1234
  • 27
  • 5
  • I didn't fully read, but `.indexOf('iPhone|iPod|iPad')` looks like it will be doing something unintended. Do you maybe want to search for `iPhone` **or** `iPod` **or** `iPad`? – ASDFGerte Aug 26 '17 at 14:45
  • i mean at all 3 when sombody call the link from iPhone or from iPad or from iPod onley IOS – d00nn1234 Aug 26 '17 at 14:47
  • just note that `"iPhone".indexOf('iPhone|iPod|iPad') === -1`. – ASDFGerte Aug 26 '17 at 14:49
  • 2
    Then you probably want something like `navigator.userAgent.match(/(iPhone|iPod|iPad)/)` – adeneo Aug 26 '17 at 14:50
  • 1
    Possible duplicate of [Detect if device is iOS](https://stackoverflow.com/questions/9038625/detect-if-device-is-ios) – James Douglas Aug 26 '17 at 14:50
  • ASDFgerta yes you have right but how to detect the IOS wenn i dont add .indexOf('iPhone|iPod|iPad') === -1 – d00nn1234 Aug 26 '17 at 14:52
  • i nead just a simple javascript when sobody call the url in a Iphone to remove in this html the Iphone text and put it IIIphone and from another devise to replace Iphone in Android – d00nn1234 Aug 26 '17 at 15:00

0 Answers0