0

I am trying to loop through a few iframes to grab the src of the iframes and remove a particular query string from the src of those iframes. This is what I have so far:

$(document).ready(function() {
   $('iframe').each(function() {
      var $src = $(this).attr('src').substring(0, $(this).attr('src').indexOf('&id'));
      alert($src);
   });
});
iframe {
  width: 300px height:300px;
  background: #fafafa;
  margin: 20px auto;
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<iframe src="mysite.com/program.asp?name=abc&status=3&id=2"></iframe>
<iframe src="mysite.com/program.asp?id=2"></iframe>

The first url of the iframe displays correctly but the second url of the second iframe does not.

The expected output:

first iframe - mysite.com/program.asp?name=abc&status=3
second iframe - mysite.com/program.asp?
al56
  • 37
  • 1
  • 1
  • 8
  • 1
    Possible duplicate of [Remove querystring from URL](https://stackoverflow.com/questions/2540969/remove-querystring-from-url) – GreyRoofPigeon Nov 07 '17 at 15:29
  • In the second function, the indexOf returns -1 so you're calculating substring(0, -1) which is an empty string. It seems normal it's not working – Axnyff Nov 07 '17 at 15:29
  • `this.src` should be accessable. I don't know why you are doing jQuery there. – Taplar Nov 07 '17 at 15:32
  • You're looking for specifically `&id=` but in the second, it's `?id=` - you need to use regex: `[&?]id=` (or call replace twice). – freedomn-m Nov 07 '17 at 15:34
  • You also have limited use cases, eg if you have : `?status=3&id=2&name=abc` then you should get `?status=3&name=abc` but you get `?status=3`. I recommend just using one of the various options in the linked duplicate. – freedomn-m Nov 07 '17 at 15:35

1 Answers1

0

UPDATE 11/7/2017 1:12PM

I was able to answer my own question and figure it out but thank you for the help and comments. Here's the expected output I mentioned before. Thanks everyone.

$(document).ready(function() {
   $('iframe').each(function() {
      if($(this).attr('src').indexOf('&id') > -1){
        var $src = $(this).attr('src').substring(0, $(this).attr('src').indexOf('&id'));
      }else{
        var $src = $(this).attr('src').substring(0, $(this).attr('src').indexOf('id'));
      }
      alert($src);
   });
});

$(document).ready(function() {
   $('iframe').each(function() {
      if($(this).attr('src').indexOf('&id') > -1){
        var $src = $(this).attr('src').substring(0, $(this).attr('src').indexOf('&id'));
      }else{
        var $src = $(this).attr('src').substring(0, $(this).attr('src').indexOf('id'));
      }
      alert($src);
   });
});
iframe {
  width: 300px height:300px;
  background: #fafafa;
  margin: 20px auto;
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<iframe src="mysite.com/program.asp?name=abc&status=3&id=2"></iframe>
<iframe src="mysite.com/program.asp?id=2"></iframe>
al56
  • 37
  • 1
  • 1
  • 8