Redeeming querystring by jquery load

0

I'm trying to call a page through load, this page will have a querystring that I want to get it on the call page, however the value comes as null when I try through the load, outside of it calling the page alone, everything is Ok.

They are 2 pages, 1 main and 2 that will bring an alert according to the value passed by the query

Main Page (index.html)

<div id="content">resultado</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script><script>functioncarrega(v){$("#content").load("/query/video.html?v="+ encodeURIComponent(v), function(response, status, xhr) {
        if (status == "error") {
            //alert("ocorreu um erro")
        } else {
            //alert("ok")
        }
    });
}

carrega("social");
</script>

Second page (video.html)

<script>
function getParameterByName(name, url) {
    if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, "\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}

var foo = getParameterByName('v');
alert(foo);
</script>

If you do the test already called the second page with the query so /video.html?v=teste123 a "test123" alert will appear, however when I try to do this through the main page passing the query parameter and bringing the query result by load, simply the error and the value comes as null.

Can anyone explain where I'm going wrong?

    
asked by anonymous 22.10.2017 / 22:05

1 answer

0

Your code is correct, the problem occurs where javascript is executed. As you make an Ajax call and play the content in "#content", the javascript runs on the same page as you are, that is, the "window.location.href" is that of the home page (index.html). You can do a test by adding "v = test" when loading the main page.

    
23.10.2017 / 01:35