lata
lata

Reputation: 1605

Unable to add parameters to url

i want to add parameters to url, for that i have 2 textbox and a button, i can't figure out where am i stuck and is unable to add parameters to url
here is my code:

HTML:

Param Name: <input id="tbAddParam" type="text" /><br>
Param Value: <input id="tbAddParamValue" type="text" /><br>
<input onclick="javascript:AddParamter();" value="Add" type="button" />

JavaScript:

function AddParamter() {
    var new_url = AddUrlParameter(window.location.href, document.getElementById('tbAddParam').value, document.getElementById('tbAddParamValue').value);
    window.location.href = new_url;
}

function AddUrlParameter(a, b, c) {
    if (b.trim() == "") {
        alert("Parameter name should not be empty.");
        return a;
    }
    if (c.trim() == "") {
        alert("Parameter value should not be empty.");
        return a;
    }
    if (a.indexOf("?") == -1) {
        return a + "?" + b + "=" + c;
    }
    var d = a.split("?");
    if (d.length >= 2) {
        if (d[1].trim() == "") {
            return d[0] + "?" + b + "=" + c;
        }
        var e = d[1].split(/[&;]/g);
        for (var f = 0; f < e.length; f++) {
            var g = e[f]; var h = g.split("=");
            if (h.length >= 2) {
                if (h[0] == b) {
                    alert("Url Parameter with provided name already exists! Try Updating that Url Parameter.");
                    return a;
                }
            }
        }
        return a + "&" + b + "=" + c;
    }
}

Upvotes: 0

Views: 463

Answers (4)

arjuncc
arjuncc

Reputation: 3287

I support @rhughes's answer. But If you test this code in IE8(I was using this and your code was not working) and below, The HTML trim() wont work. As you have trim() in 3 places, you have to use the following.

if(typeof String.prototype.trim !== 'function') {
  String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, ''); 
  }
}

or you can more easly use jQuery trim()

$.trim()

refer 1

refer 2

Upvotes: 1

james emanon
james emanon

Reputation: 11807

IE doesn't like ".trim()". I tested in IE8 with IE8 compat mode, and your code didn't work because of trim. Though I imagine IE9 is fine (as noted by Luigi's confirmation).

use something like this instead:

strReplace=str.replace(/^\s+/,'').replace(/\s+$/,''); 

You could get around it with something like:

function stripWhiteSpace(arg){
   if(arg.replace(/^\s+/,'').replace(/\s+$/,'') == ""){
      return true;
   }
}

then just call to it and pass your param

if (stripWhiteSpace(b))

Upvotes: 1

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

From the comments above:

I've tested your code and works for me (if the page ends in html, jsp or .something). This was tested using Chrome v25.

Later, I've tested on IE9 and it worked after enabling the script execution for local executed pages. A message pops up when you enter to IE. In IE9, it appears on the bottom part saying Internet Explorer restricted this webpage from running scripts or ActiveX controls. and on the right there's this option Allow blocked content..

For IE backward compatibility, it seems that you should replace the d[1].trim() as stated in james emanon's answer.

As an advice, use at least two browsers to test your web pages. And yes, I highly recommend test on IE because it will give you a good(?) feedback for being so sensitive on scripting errors (it will arise so much errors that Chrome and Firefox cover for you).

Upvotes: 1

rhughes
rhughes

Reputation: 9583

When running this in IE (I tested this in IE 10), you need to allow blocked content in order for the JavaScript to run. Otherwise the JavaScript will not be allowed to run. Luiggi Mendoza suggested the same for IE 9.

It is worth noting that this works fine in Chrome and FireFox without any user confirmation allowing JavaScript to run.

Upvotes: 1

Related Questions