ShadowMage Posted October 29, 2010 Share Posted October 29, 2010 Isn't this one of those things that only works over HTTP? Not on your desktop? Is that an issue for anyone?Actually, that is the issue. I was testing it from my Desktop. As soon as I put it on the server, it worked. So this code:var iframe = document.getElementById("iFrame_test").contentWindow.document;seems to be perfectly cross-browser (at least as far back as IE 7, anyway)Just out of curiosity, why is it that Chrome blows up when it tries to run that from the filesystem? Security? What kind of security issues can arise from that? Link to comment Share on other sites More sharing options...
jeffman Posted October 29, 2010 Share Posted October 29, 2010 Usually I can think of a way to exploit the kind of security issues most browsers protect us against. This one stumps me. Maybe I'm not creative enough? Link to comment Share on other sites More sharing options...
dsonesuk Posted October 29, 2010 Share Posted October 29, 2010 as a matter interest what was the src of your iframe linking to? a page on desktop server, or external website like google.com Link to comment Share on other sites More sharing options...
jeffman Posted October 29, 2010 Share Posted October 29, 2010 I can't speak for shadow, but I have received the security bomb even if it's 2 of my own documents living on my desktop. Link to comment Share on other sites More sharing options...
ShadowMage Posted October 29, 2010 Share Posted October 29, 2010 as a matter interest what was the src of your iframe linking to? a page on desktop server, or external website like google.comjust another page in the same folder. Link to comment Share on other sites More sharing options...
mEno Posted November 3, 2010 Author Share Posted November 3, 2010 So, now, I would like to pass parameters to the function in the following way: window.onload = function() { //Assuming I got the anchors and they are stored in the variable below for (var i=0; i<anchors_in_frame.length; i++) { anchors_in_frame[i].onmousemove = function(event){ alert(i); //some other functions follow } }} The first iteration of the above code, which happens when the page loads, I think is fine. However, afterwards when the onmousemove even is activated, the variable i keeps having its last value passed to onmousemove. I mean it's correct behaviour, but how can I make the first value of i stick in the function? Link to comment Share on other sites More sharing options...
ShadowMage Posted November 3, 2010 Share Posted November 3, 2010 I've run into this same problem before. The only way I could think of to solve it was to use...*braces self*...eval. *cringe*ie: for (var i=0; i<anchors_in_frame.length; i++) { anchors_in_frame[i].onmousemove = eval("(function(event){alert("+i+");})");} I'm almost positive that this is not a good solution, and I hope someone else can shed some light on this issue. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 3, 2010 Share Posted November 3, 2010 You can add an attribute to the element to store that value, then get it inside the function. The event object may change a little bit from browser to browser, but there will be a way to get a reference to the element that the event applies to, and then use getAttribute to get the value back. window.onload = function() { //Assuming I got the anchors and they are stored in the variable below for (var i=0; i<anchors_in_frame.length; i++) { anchors_in_frame[i].setAttribute('count_ref', i); anchors_in_frame[i].onmousemove = function(e){ var evt=window.event || e; if (!evt.target) //if event obj doesn't support e.target, presume it does e.srcElement evt.target=evt.srcElement; var i = evt.target.getAttribute('count_ref'); alert(i); } }} Link to comment Share on other sites More sharing options...
ShadowMage Posted November 3, 2010 Share Posted November 3, 2010 You can add an attribute to the element to store that value, then get it inside the function. The event object may change a little bit from browser to browser, but there will be a way to get a reference to the element that the event applies to, and then use getAttribute to get the value back.Hey, cool! I never thought to create a new attribute. See, I new there was a better way. :)Except I didn't use setAttribute/getAttribute. I just used dot syntax:var types=1;while(document.getElementById("div"+types)) { document.getElementById("div"+types).typeNum = types; document.getElementById("div"+types).onclick = function() { alert("Clicked on div"+this.typeNum); } types++;} Link to comment Share on other sites More sharing options...
mEno Posted November 7, 2010 Author Share Posted November 7, 2010 Thanks ShadowMage and justsomeguy for the creative solutions.ShadowMage, I tried that initial solution of yours and it did work but as you mentioned using that eval function was not a good idea, so I waited for a better one. Your second one does work well as well.justsomeguy, I tried your solution and it worked very well. I appreciate the help. Link to comment Share on other sites More sharing options...
dsonesuk Posted November 7, 2010 Share Posted November 7, 2010 OR you could use an alternative to eval, using 'new Function' method window.onload=function(){var F = document.getElementById("myFrame");var Fcontent = F.contentDocument || F.contentWindow.document;var anchors_in_frame = Fcontent.getElementsByTagName('a');var Flisterner = F.addEventListener || F.attachEvent; for (var i=0; i<anchors_in_frame.length; i++) { DOMcodeToRun = "alert("+i+");"; var tmpFunc = new Function(DOMcodeToRun); if(Flisterner == F.addEventListener) { anchors_in_frame[i].addEventListener("mousemove",tmpFunc,false); }else { anchors_in_frame[i].attachEvent("onmousemove",tmpFunc); } }} Link to comment Share on other sites More sharing options...
justsomeguy Posted November 7, 2010 Share Posted November 7, 2010 That's true, and works well in certain situations, but that method has all of the negative reasons for not using eval. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.