最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript switch issue with window.location-string - Stack Overflow

programmeradmin2浏览0评论

i am currently working on a simple ipad webapp testpiece and got stuck in a simple switch statement that doesn´t work somehow; i want to format elements according to javascripts window.location string. This might be embarassing simple to fix; but i somehow don´t get it: Or is there something special about the window.location?

$(document).ready(function() {
path_c = window.location;
switch (path_c){
    case "http://192.168.1.37/ryba_testground/":
    $('#menu-item-22').addClass('current_page_item');
    alert(path_c);
    break;
    case "http://192.168.1.37/ryba_testground/?page_id=7":
    $('#menu-item-21').addClass('current_page_item');
    break; 
    case "http://192.168.1.37/ryba_testground/?page_id=9":
    $('#menu-item-20').addClass('current_page_item');
    break;
    case "http://192.168.1.37/ryba_testground/?page_id=11":
    $('#menu-item-19').addClass('current_page_item');
    break;
}
});

THX!

i am currently working on a simple ipad webapp testpiece and got stuck in a simple switch statement that doesn´t work somehow; i want to format elements according to javascripts window.location string. This might be embarassing simple to fix; but i somehow don´t get it: Or is there something special about the window.location?

$(document).ready(function() {
path_c = window.location;
switch (path_c){
    case "http://192.168.1.37/ryba_testground/":
    $('#menu-item-22').addClass('current_page_item');
    alert(path_c);
    break;
    case "http://192.168.1.37/ryba_testground/?page_id=7":
    $('#menu-item-21').addClass('current_page_item');
    break; 
    case "http://192.168.1.37/ryba_testground/?page_id=9":
    $('#menu-item-20').addClass('current_page_item');
    break;
    case "http://192.168.1.37/ryba_testground/?page_id=11":
    $('#menu-item-19').addClass('current_page_item');
    break;
}
});

THX!

Share Improve this question asked Jan 17, 2013 at 10:36 thilo.devthilo.dev 2201 silver badge10 bronze badges 2
  • 5 Yes, it is a host object. Try window.location.href – Bergi Commented Jan 17, 2013 at 10:39
  • google might had helped! also a debugger..... – mercsen Commented Jan 17, 2013 at 10:43
Add a ment  | 

5 Answers 5

Reset to default 3

Or is there something special about the window.location?

It's a host object, which sometimes act unpredictable. At least, it is an object which evaluates to the href when casted to a string - yet that does not happen when paring with the switch-cases. Use

var path_c = window.location.href;

window.location returns a Location object, which contains information about the URL of the document and provides methods for changing that URL. So change it to:

var path_c = window.location.href;

I think you want the href property of the window.location object.

path_c = window.location.href;

Full Script

$(document).ready(function() {
    path_c = window.location.href;

    switch (path_c){
        case "http://192.168.1.37/ryba_testground/":
        $('#menu-item-22').addClass('current_page_item');
        alert(path_c);
        break;
        case "http://192.168.1.37/ryba_testground/?page_id=7":
        $('#menu-item-21').addClass('current_page_item');
        break; 
        case "http://192.168.1.37/ryba_testground/?page_id=9":
        $('#menu-item-20').addClass('current_page_item');
        break;
        case "http://192.168.1.37/ryba_testground/?page_id=11":
        $('#menu-item-19').addClass('current_page_item');
        break;
    }
});

Example: http://jsfiddle/9vjY9/

The code seems ok. Try to alert (window.location.href) or console.log(window.location.href), then you can check exactly what does window.location.href gets for each page you test. This might reveal the problem.

You can use window.location.toString() or window.location.href, you even can avoid using switch, with a object map and regular expression to filter your target in url:

var page_idMap = {
    '7': '#menu-item-21',
    '9': '#menu-item-20',
    '11': '#menu-item-19'
}
var id = window.location.href.match(/page_id[=]([0-9]+)/i);
if (!id || !page_idMap(id[1])) {
    $('#menu-item-22').addClass('current_page_item');
} else {
    $(page_idMap[id[1]]).addClass('current_page_item');
}
发布评论

评论列表(0)

  1. 暂无评论