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
}
}