2

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: https://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:

// https://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>
Community
  • 1
  • 1
GeminiXM
  • 23
  • 1
  • 4

1 Answers1

0

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); 
António Almeida
  • 9,620
  • 8
  • 59
  • 66
  • It works in fiddle, but I can't get it to work when I load it from my PC. I put the functions in between the tags and
    tags and saved the document as .htm and ran it in firefox - am I missing something? Thank you for the work so far Toni
    – GeminiXM Feb 04 '13 at 18:34
  • I must be doing something noobish, I just can't figure it out. If I put the code back into another fiddle it works fine, but when its all in one page on my side and I load it to the browser I get the texts and button but it doesn't do anything. Can't believe how much I've forgotten, uhg! – GeminiXM Feb 04 '13 at 19:12