简体   繁体   中英

Pass parameter to URL in javascript

I have a DIV in my .ASPX and when I click in this DIV I trigger this in my javascript code.

$("#btnFiltrar").click(function () {
    var status = "";
    var tipo = "";
    var outros = "";
    $("#divStatusImovel input").each(function () {
        if ($(this).is(":checked") == true) {
            var id = $(this).attr("id").replace("status_", "");
            switch (id) {
                case "1":
                    status += "at=t&";
                    break;
                case "2":
                    status += "in=t&";
                    break;
                case "14":
                    status += "ds=t&";
                    break;
                case "3":
                    status += "vnd=t&";
                    break;
                case "7":
                    status += "vndpr=t&";
                    break;
                case "8":
                    status += "vndtr=t&";
                    break;
                case "4":
                    status += "vndtr=t&";
                    break;
                case "9":
                    status += "vndtr=t&";
                    break;
            }
        }
    });

    $("#divTipoImovel input").each(function () {
        if ($(this).is(":checked") == true) {
            var id = $(this).attr("id").replace("tipo_", "");
            switch (id) {
                case "1":
                    tipo += "t1=t&";
                    break;
                case "2":
                    tipo += "t2=t&";
                    break;
                case "3":
                    tipo += "t3=t&";
                    break;
                case "4":
                    tipo += "t4=t&";
                    break;
                case "5":
                    tipo += "t5=t&";
                    break;
                case "7":
                    tipo += "t7=t&";
                    break;
                case "8":
                    tipo += "t8=t&";
                    break;
                case "9":
                    tipo += "t9=t&";
                    break;
                case "14":
                    tipo += "t14=t&";
                    break;
                case "15":
                    tipo += "t15=t&";
                    break;
                case "17":
                    tipo += "t17=t&";
                    break;
                case "145":
                    tipo += "t145=t&";
                    break;
            }
        }
    });

    $("#divOutrosTipos input").each(function () {
        if ($(this).is(":checked") == true) {
            var id = $(this).attr("id").replace("outros_", "");
            switch (id) {
                case "1":
                    outros += "ha=t&";
                    break;
                case "2":
                    outros += "fa=t&";
                    break;
                case "3":
                    outros += "fo=t&";
                    break;
                case "4":
                    outros += "pl=t&";
                    break;
                case "5":
                    outros += "dce=t&";
                    break;
            }
        }
    });
    var pathname = window.location;
    pathname += "?" + status + tipo + outros;
    pathname = pathname.substring(0, pathname.length - 1);
    alert(pathname);
    window.location = pathname;
});

In my last line in this javascript function, I try to take the user to the same page but passing some parameters on my URL but without success, occurs a reload in my page but don't pass the parameters on my URL.

Could anyone help me ?

UPDATE

A curiosity, when I put a debugger before the window.location.href it works

var pathname = window.location;
pathname += "?" + status + tipo + outros;
pathname = pathname.substring(0, pathname.length - 1);
debugger;
window.location.href = pathname;

If you want to read variables passed to the URL, this may be able to help you, it's what i use.

    function getQueryVariable(variable) {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
         for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] == variable) { return pair[1]; }
         }
         return (false);
    }

for example the url could be something like this: www.example.com?area=AC&interval=day

You can then fetch the variables by: var yourvariable = getQueryVariable("area");

As requested, I put together a jsFiddle with some test markup:

<div id="divStatusImovel">
    <input type="radio" id="status_4" name="status">status_4</input>
    <input type="radio" id="status_1" name="status">status_1</input>
</div>
<br/>
<div id="divTipoImovel">
    <input type="radio" id="tipo_9" name="tipo">tipo_9</input>
    <input type="radio" id="tipo_145" name="tipo">tipo_145</input>
</div>
<button id="btnFiltrar">Submit</button>

I changed the way the query string is built, and in the end, all you need to do is:

var qs_arr = [];
for (prop in qs_items)
    qs_arr.push(prop + "=" + qs_items[prop]);

var qs_str = qs_arr.join("&");

window.location.search = qs_str;

This is due to each part of the query string being built with a simple key->value map (object), like so:

var id = $(this).attr("id").replace("outros_", "");
var id_map = {
    "1" : "ha", "2" : "fa", "3" : "fo",
    "4" : "pl", "5" : "dce"
};
qs_items[id_map[id]] = "t";

See complete jsFiddle

try changing :

window.location

to

window.location.href

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM