Q:

How to stop iFrame from reloading

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.

A:

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.

