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

i am currently working on a simple ipad webapp testpiece and got stuck in a simple switch statement tha

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');
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745398296a4625981.html

相关推荐

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

    i am currently working on a simple ipad webapp testpiece and got stuck in a simple switch statement tha

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信