2

I'm trying to find a way to get a url from an iframe and using javascript modify the link and move to a another page. basicly the address in the iframe is something like "http://www.somthing.com/12/somt/arr_1.htm", using the javascript I want to remove the last five character (leaving this "http://www.somthing.com/12/somt/arr_") and than add the next logical number to it (making the link this "http://www.somthing.com/12/somt/arr__2.htm").

So far I have something like this.

    <script language="javascript" type="text/javascript">
    function urlGen(f){
                       var i1 = document.getElementById("iframe_id").contentWindow.location.href;
                       var i2 = "1.htm";
                       var i3 = "2.htm";
                       var newURL = i1 - i2 + i3
                       f.action = i1 - i2 + i3
                       return i1 - i2 + i3;
                       }
</script>

So based on what I understand from what you said it should look something like this? so it should look something like this? (keep in mind I'm the forest gump of javascript... "I'm not a smart man but I know what java is")

    <script language="javascript" type="text/javascript">
    function urlGen(f){
                       var i1 = document.getElementById("iframe_id").contentWindow.location.href = newURL;
                       newURL = il.slice(0, -5);
                       var i3 = "2.htm";

                       f.action = newURL + i3
                       }
</script>
Tom062
  • 21
  • 2

2 Answers2

0

You are grabbing the url correctly however your string manipulation isn't going to work.

You have a special function for trimming the string:

 newUrl = i1.slice(0, -5);

This will trim the last 5 characters of the url.

 newUrl = newUrl + i3;

This will concat the modified url with the new one.

 return newUrl;

now when you do:

 document.getElementById("iframe_id").contentWindow.location.href = newUrl;

It will use the new url.


With working example:

This example also incorporates the number addition:

 i1.substr(0,i1.lastIndexOf("_")+1);

This bit uses the string function substr. It is used to retrieve part of a string. The first parameter is the start location, while the second marks the end. I use the string function lastIndexOf to find the location of the last used underscore in the url, then add one to make sure it includes the underscore. The code line above will print:

http://stacksnippets.net/arr_

To build the new url, we need to now the number. parseInt helps ur here:

(parseInt(i1.substr(i1.lastIndexOf("_")+1), 10)+1)

I've put the entire part between brackets so it parses it as a sum. The first parameter of parseInt is the string containing the number (1.htm). We select this again by using substr. Now the starting location is the underscore + 1. The last argument is omitted since we can go till the end of the string. parseInt will parse 1.htm to 1. The second argument is used to force the function to use the decimal system. (radix 10). The last part of the sum adds one to the number. The result of this piece of code will be 2.

Appending it all together will give us:

http://stacksnippets.net/arr_2.htm

function urlGen(f) {
      var i1 = "http://stacksnippets.net/arr_1.htm"; //cannot grab the url because of same origin policy
      //var i1 = document.getElementById("iframe_id").contentWindow.location.href; //
      var newURL = i1.substr(0,i1.lastIndexOf("_")+1) + (parseInt(i1.substr(i1.lastIndexOf("_")+1), 10)+1) + ".htm";
      f.action = newURL;
      return newURL;
    }

     //NOT PART OF THE SOLUTION:

    document.body.onload = function() {
      document.body.innerHTML += "Document initial url: http://stacksnippets.net/arr_1.htm";
      var url = urlGen(new Object); //dummy object
       document.body.innerHTML += "<br />Changed url: " + url;
    }
Mouser
  • 13,132
  • 3
  • 28
  • 54
0

Try with this regular expression match

var i1 = "http://www.somthing.com/12/somt/arr_41.htm";

s = i1.replace(/\d+\.htm$/, function(attr) {
  return attr.replace(/\d+/, function(val) {
    return parseInt(val,10) + 1;
  });
});


alert(s);

This will change any string that ends in X.htm (where X is a digit) in the same string with X+1 in place of X (adapted from https://stackoverflow.com/a/1742841).

Community
  • 1
  • 1
user2314737
  • 27,088
  • 20
  • 102
  • 114