开发者

Jquery filter URL

I think that I am on the right track but I need your help.

I use this code to get my URL parameter

 function $urlParam(name){ 
    if(window.location.href.indexOf(name) != -1) {
        var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href); 
        return results[1] || 0; 
    }
    return 0;

} 


$(function(开发者_StackOverflow) {
var $page='#'+$urlParam('id');

 });

And than I tried to filter that data with

$side_menu.find('> li > a').filter($page).click();

But this doesn't work.

Is there anything that I can do so I can filter data with $page variable?

If you need any more data I would be glad to provide it to you.

Thank you in advance, Prokka


Ok, first off, instead of:

window.location.href

you might as well use:

window.location.search

as that is just the parameter part of the URL (the part you want). Also, your variable:

$page

... the "$foo" variable naming convention is usually meant to be applied to jQuery object variables. In other words:

var foo = "foo";
var $foo = $(foo);

So (unless you have your own, different convention) I think "$page" should really be named page .

As for:

$side_menu.find('> li > a').filter($page).click();

I can't tell for sure, but it looks like you're filtering on an id. Is that correct? If so, filter makes no sense; IDs are by definition unique on the page, so simply doing:

$($page).click();

is totally the same thing (even performance-wise).

But this doesn't work. Is there anything that I can do so I can filter data with $page variable?

Without knowing the value of your page variable it's hard to say. I'd suggest adding this:

console.log($page);
$side_menu.find('> li > a').filter($page).click();

Well, if you're using Firebug or a browser with a console (if not ... you should be using Firebug ;-) but you could also just change it to "alert($page)").

When you do that, you should see what selector you're really using. Most likely something is wrong with it. If you don't see anything obvious, try adding:

console.log($page); console.log($($page));
$side_menu.find('> li > a').filter($page).click();

(the alert trick won't work for this one). This should show you what elements (if any) jQuery selected using your selector. Between this and reviewing the selector itself, you'll likely find the problem (and if you don't, maybe try playing around with running the commands in the Firebug console manually)


Something like:

<script>
// Define event handler generator
function handleClick(url) {
    var location = url;
    return function() {
        window.location = location;
        return false;
    }
}
$(function () { // onReady
    $("A").each(function() { // connect an event handler for every A
        // Generate handler for the correct URL
        var handler = handleClick($(this).attr("href"));
        // Connect generated event handler
        $(this).click(handler);
    });
}
</script>

Should let you keep your existing URLs working the way they already do (for non-JS users), but for everyone else you'll have an event you trigger which does the same thing, but which is jQuery-invoke-able.


Thank you all so much!

Finally I figure out my mistake... I define page variable after page is loaded, so it wasn't readable for my script.

Now it looks like this:

     function $urlParam(name){ 
    if(window.location.href.indexOf(name) != -1) {
        var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href); 
        return results[1] || 0; 
    }
    return 0;


};

And after that

var page='#'+$urlParam('id');
if (page== '#0')
     strana ='#News';


$side_menu.find('> li > a').filter($(page)).click();

And now almost everything works great except Google Chrome which only select tab but don't show it content. I read here jQuery find() returning an empty string in Google Chrome that it is about Chrome and HTML in Ajax.

Any tip for this or link or to make another question?

Thank you all one more time, specially machineghost!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜