0

I have access to a url runs a script to clear a users state. Id like to produce a script that will run on a webpage, to do this.

The following script works but in Firefox its annoying for people to have to disable mixed content each time they come to the page:

 <SCRIPT LANGUAGE="JavaScript">
function go(loc,loc2){
    document.getElementById('userstate').src = loc +
    document.getElementById('username').value +  loc2;
}
</script>

<form onSubmit="go('http://mysiteurlomitted.com/userstate/?userId=','&app=cc'); return false;"/>
Username: <input type="text" id="username">
<input type="submit" value="Clear User State">
</form> 

<iframe id="userstate" src="about:blank" width="470" height="30" frameborder="1" scrolling="no"></iframe>

The resulting URL produces a simple text string, and has no HTML on the results page, so I feel it should be pretty easy to read this URL as a file, and load the results into an alert box. This would avoid the iframe method, and get out of the mixed content situation. It would run without anyone needing to change anything. But I cannot figure out how to get this to work. I feel like FileReader() should be a good way to do it, but the URL has parameters... so the reader doesn't know what file type it is. Its just failing. There has to be an easier way to do this.

Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
  • Can this solution help you? http://stackoverflow.com/questions/926916/how-to-get-the-bodys-content-of-an-iframe-in-javascript – Nickolay Kondratenko Mar 19 '14 at 17:17
  • If my intention is to not use an iframe and instead use an alert box to display the results, that solution wont work Nickolay, thanks though. – consequence Mar 19 '14 at 17:59

1 Answers1

0

You shouldn't need any javascript for this kind of thing. Just add a name attribute to the username input, and that value will get passed along in the query string as "&username=Name". so:

<form action="/userstate?app=cc">
    Username: <input type="text" id="username" name="username" />
    <input type="submit" value="Clear User State" />
</form>

That would submit the form to "http://samedomain.wut/userstate?app=cc&username=", which would be available on the server side in the usual post data source.

There are other, better ways of doing this, but I don't know your setup. You should look into server-side management of cookies/session; the user shouldn't have to input anything except to log in initially.

Edit:

If you want to use this as an administration tool, you can do two things: use ajax (XMLHttpRequest or $.ajax from Jquery), or make your endpoint redirect back to (or serve) the form. You could have the form submit to its own url and in your server side scripting process the data, then output the html for the form again. Not a great pattern for actual applications, but it should work in this situation.

jstaab
  • 3,449
  • 1
  • 27
  • 40
  • Id like it to load the results into an alert box on the page I put the script on. I want it to run in a way where I could type a username, click clear user state. (alert comes up: close that) enter another username, click clear user state. And it never leave that page, just allowing me to do it over and over. Rearranging the url parameters is good though. – consequence Mar 19 '14 at 17:44
  • Gotcha, gotcha, so this is more of an administration tool. I'll edit my answer. – jstaab Mar 19 '14 at 18:19