www.webdeveloper.com
Results 1 to 4 of 4

Thread: Switch Help

  1. #1
    Join Date
    Jun 2012
    Posts
    4

    Switch Help

    I'm making a bookmarklet that has different variables stored in the switch cases that I want to call depending on what webpage the bookmarklet is used on.

    javascript:
    page=window.location;
    switch(page){
    case "http://page1.html": a=1;b=2;c=3;break;
    case "http://page2.html": a=3;b=2;a=1;break:
    default: alert("window.location= "+page+"");
    };a+b-c=d;$('#box1').val(d)

    This isn't my exact code I tested the bookmarklet without the switch in it and it worked perfectly but when I tried to use the switch all i get is the alert that tells me the page url. I set the default to alert to tell me the page url to see if I put the wrong url in for the case but that wasn't the problem.

    Am I just using switch wrong or did I mess up in the coding?

  2. #2
    Join Date
    Mar 2011
    Posts
    1,148
    If the code you posted is exactly what you're using, then the problem is that 'page' is never going to equal "http://page1.html" or "http://page2.html". You need to use the complete URL including the domain name and any directory names.

  3. #3
    Join Date
    Jun 2012
    Posts
    4
    Quote Originally Posted by rtrethewey View Post
    If the code you posted is exactly what you're using, then the problem is that 'page' is never going to equal "http://page1.html" or "http://page2.html". You need to use the complete URL including the domain name and any directory names.
    That was an example of what my code is like. I know that the exact code had wouldn't work.
    This is my real code but unless you play that game it cant be tested
    javascript:
    village=window.location;
    switch(village){
    case"http://en63.tribalwars.net/game.php?village=33298&screen=place": sp=0;sw=0;ax=0;ar=0;spy=1;lc=5;ma=0;hv=0;cat=0;ram=0;no=0;kn=0;coords="123|321 456|654 789|987";break;
    case"http://en63.tribalwars.net/game.php?village=33299&screen=place": sp=0;sw=0;ax=0;ar=0;spy=1;lc=2;ma=0;hv=0;cat=0;ram=0;no=0;kn=0;coords="123|321 456|654 789|987";break;
    default: alert("window.location= "+village+"");};coords=coords.split("%20");index=0;farmcookie=document.cookie.match("(^|?"+6333298+"=([^;]*)(;|$)");if(farmcookie!=null)index=parseInt(farmcookie[2]);if(index>=coords.length)alert("Last%20Village");if(index>=coords.length)index=0;coords=coords[index];coords=coords.split("|");index=index+1;cookie_date=new%20Date(2015,11,11);document.cookie=6333298+" ="+index+";expires="+cookie_date.toGMTString();$('#inputx').val(coords[0]);$('#inputy').val(coords[1]);$('#unit_input_spear').val(sp);$('#unit_input_sword').val(sw);$('#unit_input_axe').val(ax);$('#uni t_input_archer').val(ar);$('#unit_input_spy').val(spy);$('#unit_input_light').val(lc);$('#unit_input _marcher').val(ma);$('#unit_input_heavy').val(hv);$('#unit_input_ram').val(ram);$('#unit_input_catap ult').val(cat);$('#unit_input_knight').val(kn);$('#unit_input_snob').val(no);$("#target_attack").foc us();end();

    I wrote this to try to figure out what I was doing wrong but I get the same problem as with my other code.
    javascript:
    sites=window.locaton;
    switch(sites){
    case"https://www.google.com/": a="1";break;
    case"http://www.yahoo.com/": a="2";break;
    default: alert("window.location= "+sites+"");
    };alert(a);end;
    Last edited by jhatear; 07-01-2012 at 02:48 AM.

  4. #4
    Join Date
    Jun 2012
    Posts
    4
    i just switched to a if else statement and it works fine now

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles