#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    10
    Rep Power
    0

    Url parameter not alerting out in jQuery Mobile script


    After clicking on an article link in my first html page, the URL of the second page looks like this:

    localhost/basket/newstext.html?url=basket-planet.com/ru/news/9235

    In JS, I'm trying to get the url=... and do something with it. Here's what I have for the scripts:

    page2.html:

    <body>
    <div data-role="page" id="newstext">
    <div data-role="content">
    <div id="textcontent"></div>
    </div>
    </div>
    <script src="js/newstext.js"></script>
    </body>

    newstext.js:

    $('#newstext').bind('pageshow', function(event) {
    var url = getUrlVars()["url"];
    alert (url);
    });

    function getUrlVars() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
    hash = hashes[i].split('=');
    vars.push(hash[0]);
    vars[hash[0]] = hash[1];
    }
    return vars;
    }

    If you need to see my first page along with js and php file, let me know. Strange thing is I had it working before and now...I don't know what happened. I tried placing the script between the head tags, doesn't work. What's wrong here?
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,187
    Rep Power
    184
    Are you trying to do something like this?
    Code:
    <script>
    function getUrlVars() 
    {
    var query_string = document.location.href.split("url=")[1];
     if (query_string != undefined) {
      alert(query_string);
     }
    }
    window.onload = function() {
    getUrlVars();
    }
    </script>
    Test this code out, with your "url" variable and query string ("localhost/basket/newstext.html?url=basket-planet.com/ru/news/9235"). If it's not; try to give me some more details and we can go from there.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    10
    Rep Power
    0
    Still nothing. I'm using jQuery Mobile, that's why my functions look like that, especially the .bind function. Crazy thing is this worked for me before!!!

    The initial page gets article with their links. When the user clicks on the article, page2 loads and with bind.('pageshow'...) the function has to get the parameter of the article, which is appended to the page2 url. In the console, XHR just has the entire URL, and sometimes it has a Status 304 Not Modified. No other errors...
  6. #4
  7. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,187
    Rep Power
    184
    Try:
    Code:
    $('#newstext').bind('pagechange', function() {
    getUrlVars();
    });
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    10
    Rep Power
    0
    Can you please post the entire code? I mean the first part, how you would alert it out. I need it in a variable so I can do something with it later.

    I can't get any variation to work...same console output also, no errors.
  10. #6
  11. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,187
    Rep Power
    184
    Ok, then let's try a different route; try this:
    Code:
    <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.2.1/jquery.mobile-1.2.1.min.js"></script>
    <script>
    function fakeXHR()
    {
    window.scrollTo(0,0);
    var my_xhr = "This Would Be Your XHR Response Text";
    $("#textcontent").html(my_xhr).trigger("contentchange");
    }
    </script>
    
    <div data-role="page" id="newstext">
    <div data-role="content">
    <div id="textcontent"></div>
    </div>
    </div>
    
    <script>
    $('#textcontent').bind("contentchange", function() {
    var query_string = document.location.href.split("url=")[1];
     if(query_string != undefined) {
      alert(query_string);
     }
    });
    </script>
    
    <a href="?url=basket-planet.com/ru/news/9235" onclick="fakeXHR()">Add XHR Content</a>
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    10
    Rep Power
    0
    Thanks for your advice but I kind of figured it out. The problem was and is that when I link to the second page, jQuery Mobile sends an AJAX request from the first page and gets the second page. So the response is the entire second page. What I did was turn off AJAX page requests and the script worked perfectly. I'm trying to figure out a way around it but at least now It alerts out the correct url parameter.

IMN logo majestic logo threadwatch logo seochat tools logo