2

I am creating a submittal form, sending the form to a php form, and after the form completes having it redirect to the initial page with an additional "?s=1" in the url. Basically what I am trying to do is create an alert box pop up on loading the page with the "?s=1" in the url.

It is a very brute force method to use I know, but i can't seem to get the small script to work correctly. I know for certain everything works and loads to the point and reloads the initial page with ?s=1 in it.

Here is the code i'm using to try and prompt the alert box

enter code here <script type="text/javascript">
        var Path = window.location.href;
        if (Path == "mywebsite.html?s=1")
            {
            alert("Your Form Has Been Submitted.")

            }
        else()
            {

            }
    </script>

Does anybody know why the box will not appear? Or possibly an alternate method for what I am attempting to do? Thanks.

  • possible duplicate of [How can I get query string values in JavaScript?](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – famousgarkin Feb 24 '15 at 21:03

3 Answers3

2

window.location.href contains the complete URL, including the domain, and the full path, so a basic equality comparison won't work unless you're exaclty matching it, and even still this could cause problems (e.g. www. versus a naked domain, https:// versus http://, etc.). A possible solution is to use RegEx.

 var pathRegex = /mywebsite\.html\?s\=1/;
 if (pathRegex.test(window.location.href)) {
     alert("Your Form Has Been Submitted.")
 }

As a note, you can have an if statement without an accompanying else, and else statements don't take any arguments in parentheses like if, unless you're talking about else if.

Brandon Anzaldi
  • 6,884
  • 3
  • 36
  • 55
  • Thank you very much. Implemented flawlessly. I was thrown a project with no background in html php or javascript and have been learning the languages on the fly, but again thank you so much! – falcons1112 Feb 24 '15 at 21:51
  • @falcons1112 Glad I was able to help :) Though as you move on, you should look into flash messages, a more infallible, easily extensible way to show messages to your users. :) https://github.com/plasticbrain/PHP-Flash-Messages – Brandon Anzaldi Mar 03 '15 at 23:17
1

Here is some code I wrote up for one of my projects that lets you pull a parameter and value out of the url.

 function GetURLParameter(urlParameter){ 
        var url = window.location.search.substring(1); 
        var urlVariables = url.split('&'); 
        for (var i = 0; i < urlVariables.length; i++){ 
            var parameter = urlVariables[i].split('='); 
            if (parameter[0] == urlParameter){ 
                return parameter[1]; 
            } 
        } 
    } 

It's easy to use:

For mywebsite.com?s=1

It would just be

var k = GetURLParameter('s');
if (k == 1){
    alert("Your Form Has Been Submitted.")
}
user1270657
  • 137
  • 1
  • 4
  • 14
0
function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

And then check like...

    if (getParameterByName("s")=="1")
                {
                alert("Your Form Has Been Submitted.")
                }
            else
                {

                }
void
  • 36,090
  • 8
  • 62
  • 107