简体   繁体   中英

javascript : window.location.href/window.open is not working on iframe

Updated Code

<div>
    <button type="button" class="submit btn btn-default" id="btnSubmit">Submit 
    </button>
    <button type="button">Cancel</button>
</div>




<script>
$("#btnSubmit").click(function(e)
 {
  e.preventDefault();

  //btn Click validation and code submission goes here..

   alert("Form has been successfully submitted");
   window.open("http://....../viewmyrequest.aspx","_self"); 

  }

</script>

window.location.href is working only on debug mode and redirect to other window is not happening if i turned off debug mode.

i have explored many StackOverflow questions with the same topic but i haven't got any solid response for this issue.

window.location.href = "http://testwebsite.com/viewtickets.aspx"

i have tried for other options too

window.location = "http://testwebsite.com/viewtickets.aspx"
window.location.replace = "http://testwebsite.com/viewtickets.aspx"

Try this. Works cross browser in all my sites.

loc = "http://testwebsite.com/viewtickets.aspx";
    window.open(loc,'_self');

EDITED: This may work better for you..

<script>
function setURL(url){
    document.getElementById('iframe').src = url;
}
</script>
<iframe id="iframe" src="idreesinc.com/research.html" />
<input type="button" onclick="setURL('URLHere')" />

It will insert the correct URL into the iframe.

I am 99% sure the issue is you are using a submit button and that is submitting the form which is not allowing your JavaScript to run. So either you need to set the type to be a button and not a submit button OR you need to cancel the click action.

<button type="button" ... >

or

<button onclick="foo(); return false;" ... >

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM