GeminiXM GeminiXM - 1 year ago 109
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:

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:


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



<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;;h=AfjSxEzzdTSrz-pS&amp;from_gear=timeline" rel="dialog">
<span class="itemLabel fsm">Report/Block...</span></a></li></ul></div>
</div> -->

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

Answer Source

Your almost done it, look to this fiddle, its working:

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


<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); 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download