0

The goal of this code is to check for a consecutive &, b, u, and =. However, when I input my code through Javascript injection, it crashes the webpage.

Code:

var str = document.URL; //gets URL of webpage
var copied = 0;

//this loop reads the URL character by character and checks if it is an &, b, u, or =. If so, it sets the corresponding variables to 1.
for (var i = 0; i < 1; i++) {
    var res = str.charAt(i);
    if (res == "&") {
        var ampYes = 1;
    } else {
        var ampYes = 0;
    }
    if (res == "b") {
        var bYes = 1;
    } else {
        var bYes = 0;
    }
    if (res == "u") {
        var uYes = 1;
    } else {
        var uYes = 0;
    }
    if (res == "=") {
        var eqYes = 1;
    } else {
        var eqYes = 0;
    }
    alert(res)
}

I presume that the reason it's crashing is due to an error in one of my "if"s. I tested it on a website without an ampersand and it didn't crash. However, on a website with a consecutive &, b, u, and =, it crashed the page.

5
  • 2
    Pretty sure it isn't your code crashing, it will only loop through the for loop once. Commented Jan 9, 2014 at 15:33
  • Can you explain what you mean by "crashing" and "crash the page"? Do you get errors in the console? If so, exactly what errors do you get? Usually, "crash" means the whole browser process dies. Commented Jan 9, 2014 at 15:33
  • Can you explain what your code is supposed to do? You're looping over only a single iteration...You set some local variables that never get used. I don't understand what this was supposed to accomplish. Commented Jan 9, 2014 at 15:35
  • Do you have an url example that crashes? Commented Jan 9, 2014 at 15:44
  • Using Mike 'Pomax' Kamerman's answer, I was able to fix my code. Thank's for the concern, though! Commented Jan 9, 2014 at 16:37

2 Answers 2

5

Don't invent your own wheel. Use url.indexOf("&bu=") > -1 to see if it's in there.

Sign up to request clarification or add additional context in comments.

2 Comments

I don't think url.indexOf("&bu=") > -1 will return true with url u=foo, for a case like this, it's better to use match with a regex. indexOf is a great solution, but it as to look for a single char at once like if (url.indexOf("&") > -1), etc...
no, but the question states the author's trying to find a consecutive &, b, u and =, which we do with indexOf("&bu="). How they then implement it becomes fairly irrelevant because there's a clean JS-native way to do this.
1

Here is simple solution with regex:

var url = document.URL,
    ampYes, bYes, uYes, eqYes;

ampYes = /\&/gi.test(url) ? 1 : 0;
bYes = /b/gi.test(url) ? 1 : 0;
uYes = /u/gi.test(url) ? 1 : 0;
eqYes = /\=/gi.test(url) ? 1 : 0;

console.log(url, ampYes, bYes, uYes, eqYes);

You can try changing the value of url and testing in here: http://jsfiddle.net/kq82D/

Good luck!

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.