Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
85
rated 0 times [  87] [ 2]  / answers: 1 / hits: 21771  / 13 Years ago, wed, july 13, 2011, 12:00:00

I am trying to pass a parameter to an iFrame by adding a param in the onload event as below:



<iframe name=myframe src=http://test.com/hello onload= frames['myframe'].location.href='http://test.com/hello?uname='+getUserName();>

</iframe>


(getUserName is my custom function)



But the iFrame keeps loading over and over again. How do I stop this using just javascript (and not any js framework)?



Note:For certain reasons, I can write only inline javascript scripts.


More From » iframe

 Answers
8

You can set a global flag (choose an appropriate name that does not collide with any other name):



if (window.frames['myframe'] && !window.userSet){
window.userSet = true;
frames['myframe'].location.href='http://test.com/hello?uname='+getUserName();
}


Alternatively you can set a custom attribute to the element:



var userSet = this.getAttribute('data-userset');
if (window.frames['myframe'] && !userSet){
this.setAttribute('data-userset', 'true');
frames['myframe'].location.href='http://test.com/hello?uname='+getUserName();
}


Or if you don't have to invoke the event handler later on again, you can remove it:



if (window.frames['myframe']){
this.onload = null;
this.setAttribute('onload', '');
frames['myframe'].location.href='http://test.com/hello?uname='+getUserName();
}


Btw. what do you have the if statement for? I think it will always be true anyway.


[#91210] Tuesday, July 12, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jonrened

Total Points: 627
Total Questions: 114
Total Answers: 99

Location: Zimbabwe
Member since Thu, Jul 21, 2022
2 Years ago
jonrened questions
Mon, Nov 2, 20, 00:00, 4 Years ago
Tue, May 19, 20, 00:00, 4 Years ago
Tue, Jan 21, 20, 00:00, 4 Years ago
Thu, Nov 7, 19, 00:00, 5 Years ago
;