GeminiXM GeminiXM - 2 months ago 11
HTML Question

Read 'hidden_elem' comments using JavaScript

I'm trying to narrow down some web code for a new site I'm making. I found this answer that seems to be what I am looking for, but I can't get it to work: http://stackoverflow.com/questions/13483194#13483649

I was trying the non-jQuery answer, but I seem to be missing something, as it doesn't do anything when I load the page into Firefox and press the button. I am using phpDesigner software, copy pasting into a .htm file and then running in Firefox.

Here is the code I did:

JavaScript:

// http://stackoverflow.com/a/5158301/74757
function getParameterByName(name, path) {
var match = RegExp('[?&]' + name + '=([^&]*)').exec(path);
return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}

var hiddenElHtml = document.getElementsByClassName('hidden_elem')[0].innerHTML.replace('<!--', '').replace('-->', '');

var divObj = document.createElement('div');
divObj.innerHTML = hiddenElHtml;

var itemAnchor = divObj.getElementsByClassName('itemAnchor')[0];
var href = itemAnchor.getAttribute('href');

var fbId = getParameterByName('h', href);

alert(fbId);


HTML:

<code class="hidden_elem" id="ukftg4w44">
<!-- <div class="mtm mlm">
...
....
<span class="itemLabel fsm">Unfriend...</span></a></li>
<li class="uiMenuItem" data-label="Report/Block...">
<a class="itemAnchor" role="menuitem" tabindex="-1" href="/ajax/report/social.php?content_type=0&amp;cid=1352686914&amp;rid=1352686914&amp;ref=http%3A%2F%2Fwww.facebook.com%2F%3Fq&amp;h=AfjSxEzzdTSrz-pS&amp;from_gear=timeline" rel="dialog">
<span class="itemLabel fsm">Report/Block...</span></a></li></ul></div>
....
...
</div> -->
</code>

<button onclick="getParameterByName()">Try it</button>

Answer

Your almost done it, look to this fiddle, its working:
http://jsfiddle.net/promatik/qyeWT/

I put all your code in a function, getAttributes() and I've added another attribute just to better test your problem:

var fbId = getParameterByName('h', href);
var cId = getParameterByName('cid', href);

document.getElementById('parameter-h').innerHTML  = fbId;
document.getElementById('parameter-cid').innerHTML  = cId;

alert(fbId + "\n" + cId);

HTML:

<div><span>HREF = </span><span id="parameter-h"></span></div>
<div><span>CID = </span><span id="parameter-cid"></span></div>
<button id="tryagain">Try it</button>

And the button has an eventlistener:

document.getElementById("tryagain").addEventListener("click", getAttributes, false);