1

I have a site that relies on a JSON file that PHP parses to output a navbar (I built an editor so the customer can maintain content). A link in the JSON file has this structure:

"Text to Display": {
    "href": "myPage.php",
    "child": {
        "Text for Child Link": {
            "href": "secondPage.php",
            "child": {}
        }
    }

And a recursive function (PHP) to loop over it and output the navbar such as:

function block_menuList($links) {
    $o='';
    foreach ($links as $k => $v) {
        $href='javascript:void(0)'; // default to link nowhere
        if (isset($v['href'])) $href=$v['href']; // pull link from array
        $o.="<li><a href='$href'>$k</a>"; // output list item
        if (isset($v['child']) && is_array($v['child'])) $o.="<ul>".block_menuList($v['child'])."</ul>"; // if there's a child list call recursively
        $o.="</li>";
    }
    return $o;
}

The site runs on a hash model system, and I'd like to be able to add links to change the hash model without having to switch this to conditionally use onclick instead of href. The javascript is not responding how I expect it to. I should be able to correctly use:

<a href="javascript:location.hash=buildHash('c=8')">My Link</a>

Instead of working, however, things like this happen:

  • Firefox's URL bar says javascript:location.hash=buildHash('c=8') and the content is #c=8, and hitting the back arrow takes me to the expected target.
  • Chrome kept the URL bar the same, but replaced the body's HTML with #c=8

I can define the link like this and everything works fine:

<a href="javascript:void(0)" onclick="location.hash=buildHash('c=8')">My Link</a>

Calls to buildHash from javascript work as well, for example in JQuery event listeners. Only the <a href="javascript:something()"> method seems to be broken, but a simple test of <a href="javascript:alert('yu no build hash?')"> runs and shows the alert box.

edit: Due to this structure I won't be able to bind JS/JQuery events. Here's the full object I'm using on site right now so my issue is easier to understand:

{
    "Home": {
        "href": "index.php",
        "child": {
            "About Us": {
                "href": "view.php?pg=about",
                "child": {}
            },
            "Contact Us": {
                "href": "view.php?pg=contact",
                "child": {}
            },
            "Location & Hours": {
                "href": "view.php?pg=location",
                "child": {}
            },
            "News": {
                "href": "news.php",
                "child": {}
            }
        },
        "parent": true
    },
    "Products": {
        "href": "cart.php",
        "child": {
            "categories": {
                "href": "categories",
                "child": {}
            }
        },
        "parent": true
    },
    "Sales": {
        "href": "javascript:filterModel.goto(\"sl=1\", \"\")",
        "child": {},
        "parent": true
    },
    "Clearance": {
        "href": "javascript:filterModel.goto(\"cl=1\", \"\")",
        "child": {},
        "parent": true
    },
    "My Account": {
        "href": "account.php",
        "child": {
            "Order Status": {
                "href": "ordStat.php",
                "child": {}
            },
            "Order History": {
                "href": "ordHist.php",
                "child": {}
            }
        },
        "parent": true
    }
}
MaKR
  • 1,882
  • 2
  • 17
  • 29
  • Have you tried `javascript:(function(){location.hash=buildHash('c=8');})()` ? – towr Dec 26 '13 at 20:17
  • Is it possible to use jQuery? It smooths out a lot of browser irregularities. – Katie Kilian Dec 26 '13 at 20:18
  • in the `javascript:` link, try adding `void(0)` at the end – markasoftware Dec 26 '13 at 20:25
  • towr: didn't work, some error about functions must have names. charlie: no, the customer will be editing the link location at times through an admin section, the simpler the better. I solved by making a function gotoCategory(cat) which calls buildhash with "c="+cat args, which did work. – MaKR Dec 26 '13 at 20:35
  • correct way: http://stackoverflow.com/a/9440536/986862 – Andre Figueiredo Feb 04 '14 at 12:58
  • That's absolutely not going to work. My situation is a very specific use case. I'm familiar with JS/JQuery binding, and if it was an option I would have used it. Post edited for full sample of an object I'm reading from. – MaKR Feb 05 '14 at 00:20

0 Answers0