0

This is the link of the page:

localhost/basket/newstext.html?url=http://www.basket-planet.com/ru/news/9246

When the page is loaded, I'm trying to alert the url so I know it's in a variable. But it's not. I found many methods and none work for me. I don't understand why!!! My html page:

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

js script:

$('#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;
}

I'm desperate to get an answer to this...PLEASE HELP OUT!!!

Update: Here is the js script which appends the links and articles on the first page:

var serviceURL = "http://localhost/basket/services/";
var news;
$('#mainNews').bind('pageinit', function(event) {
getNews();
});

function getNews() {
$.getJSON(serviceURL + 'getmainnews.php', function(data) {
    $('#fnews li').remove();
    mainnews = data.items;
    $.each(mainnews, function(index, mnews) {
        $('#fnews').append('<li data-icon="false"><a style="white-space:normal;" href="newstext.html?url=http://www.basket-planet.com' + mnews.link + '">' + mnews.article + '</a></li>');
    });
    $('#fnews').listview('refresh');
});
}
user2025469
  • 1,531
  • 2
  • 14
  • 26
  • What server side language are you building that query string in? It needs to be URL encoded. – Mathew Thompson Apr 05 '13 at 21:43
  • 1
    I tested the code, and it works fine. Do you include jQuery? What are you using to get the `pageshow` event to trigger? – Guffa Apr 05 '13 at 22:06
  • On the previous page, I get the data from a database (article link and article text). I make a .getJSON call to a PHP file and append the results in a jQuery Mobile listview. – user2025469 Apr 05 '13 at 22:06
  • So you want to pass parameters between pages through URL, in jQuery mobile? This [answer](http://stackoverflow.com/questions/15765858/jquery-mobile-navigate-why-is-the-state-empty/15768060?noredirect=1#comment22530839_15768060) could be of help if you're using `$.mobile.changePage()` method. – Omar Apr 05 '13 at 22:10
  • I updated the question with js script from my initial page, where the user would click on an article. The first page loads perfectly and the URL of the second page is as it should...but no functions work. Is it something with jQuery Mobile? – user2025469 Apr 05 '13 at 22:15
  • You say that the code doesn't alert the url, but what does it do? Does it alert anything, and if so, what? – Guffa Apr 05 '13 at 22:29
  • It doesn't do anything...just displays an empty page. Please look in the comments below...I explained when it sometimes alerts Undefined, and that's only when I include that script in the first page. – user2025469 Apr 05 '13 at 22:35
  • 2
    http://stackoverflow.com/a/15840673/1848600 – Gajotres Apr 05 '13 at 22:42
  • 2
    @Gajotres MAN!!! WHERE WERE YOU EARLIER??? I'll try your answer tomorrow but from skimming it looks like what I need. – user2025469 Apr 05 '13 at 22:48
  • Tell me, did it help? – Gajotres Apr 06 '13 at 13:20
  • I ended up keeping it the way I figure it out. I just turned off ajax page loading and it works every time. I can send the url parameter to my php file and get a response. I guess it defeats the purpose of using jQuery Mobile but I'll add some transitions later to make it look nice – user2025469 Apr 07 '13 at 16:54

3 Answers3

1

I finally got it to work. From seeing how the page transitions were being handled, and I should have rememberd when I was reading about it, jQuery Mobile handles everything through AJAX. The fix was to add $.mobile.ajaxEnabled = false; in the first page so the pages would handle like normal web pages.

If someone can add to this and tell me how this could with with AJAX enabled, that would be great. But with much struggling, that's the only solution I found. Thanks all for helping!

user2025469
  • 1,531
  • 2
  • 14
  • 26
0

I encountered that problem a week from now and here's what i've done try this:

function GetURLParameter(sParam)
{
    var sPageURL = window.location.search.substring(1);
    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++) 
    {
       var sParameterName = sURLVariables[i].split('=');
       if (sParameterName[0] == sParam) 
       {
        return sParameterName[1];
       }
    }
}​

and call this function :

 var tech = GetURLParameter('technology');

thanks to http://jquerybyexample.blogspot.com/2012/06/get-url-parameters-using-jquery.html

give it a try:)

mCube
  • 334
  • 3
  • 8
-1

If you just want to get the current URL using Javascript, this is the simplest way to do it.

<html>
  <body>
    <script>alert(window.location);</script>
  </body>
</html>
fisharebest
  • 1,300
  • 10
  • 16
  • 1
    The OP is not looking for the URL of the page, but a querystring value with the key "url". – Guffa Apr 05 '13 at 21:58