1

I have written a script that opens always, more or less, the same link but with another ID. Which is the variable in the loop. My script looks actually like this and I would only like to know how I can open this link with the variable as an ID:

<!DOCTYPE html>
<html>
<body>

<button onclick="openLinks();">Click </button>

<script>
    function openLinks() {
        var i;

        for (i = 150; i < 156; i++) {
            window.open('http://www.someurl.at/wp-admin/admin.php?page=wpsl_store_editor&action=edit_store&store_id="i"'); //doesn't work, should print 150, 151...
        }
    }
</script>

</body>
</html>
4
  • Try this in the console: "store_id="+3 Commented Jul 21, 2015 at 17:46
  • 3
    Pop up blocker will block that.... Commented Jul 21, 2015 at 17:46
  • That doesn't work. And no, the PopUp Blocker is no problem, I am using it for personal use and I deactivated the blocker for this html site Commented Jul 21, 2015 at 17:50
  • @epascarello Will a popup blocker will block it due to actions per time or due to many actions or due to window.open which is not via user click ( and if so , what about 2...n clicks)? Commented Jul 21, 2015 at 18:24

5 Answers 5

1

It looks like you're not adding the variable as a string to your url.

Try something like this:

 window.open("http://www.ortner.elmima.at/wp-admin/admin.php?page=wpsl_store_editor&action=edit_store&store_id=" + String(i));
Sign up to request clarification or add additional context in comments.

4 Comments

That's not necessary at all.
No problem. :-) @SebastianNette How so?
@aznbanana9 Because you can concat strings and integers just fine. "text" + i is already a string. String(i) and ""+i pretty much do the same.
Agreed, though for the sake of good practice, would you say it is better to explicitly call the global constructor String?
0

If you put double quotes inside a single quote JS engine will understand it as a regular character not a variable. " and ' is string literals, however, you should use only one of them.

You should write something like this

var link = "someurl" + i;

Comments

0

How would you build any normal string?

var x = "asdfg" + i;

so what you are doing is no different.

....tor&action=edit_store&store_id=' + i)

Comments

0

You have string/number concatenation problem :

Change :

 window.open('http://www.someurl.at/wp-admin/admin.php?page=wpsl_store_editor&action=edit_store&store_id="i"');

To

 window.open('http://www.someurl.at/wp-admin/admin.php?page=wpsl_store_editor&action=edit_store&store_id='+i);

The i within store_id="i"' is not dynamically evaluated.You have to "take out" the i as a string to a dynamic loop variable which concats to the string.

Comments

0

It should be

window.open('http://www.someurl.at/wp-admin/admin.php?page=wpsl_store_editor&action=edit_store&store_id="' + i + '"');

so that i does not render as part of a string, but rather to mean the variable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.