Jump to content

document.location.href is not execute without alert popup using java script


hb_venkatesh
 Share

Recommended Posts

Hi Expert, I have a problem for execute the hyperlink in javascript. When i have alert command in for loop "document.location.href " is execute properly , if i remove the alert command. it will not working. Please adivce on the below script, how to over come this issue.

 <HTML><HEAD><script type="text/javascript">function DoTheCheck(form){for (i=0; i<document.forms["DetailBlock"].p_dmmy_box.length; i++){if (document.forms["DetailBlock"].p_dmmy_box[i].checked==true){alert("Checkbox at index "+i+" is checked!")var p1=document.forms["DetailBlock"].p1.value;var ps=document.forms["DetailBlock"].ps.value;document.location.href ="https://dev.xxx.com/x/test.load?p1="+p1+"&ps="+ps}}</script></HEAD><BODY MARGINWIDTH="0" MARGINHEIGHT="0" LEFTMARGIN="0" TOPMARGIN="0" bgcolor="#ffffff"  link="#993300" vlink="#666666" alink="#FFCC00"><FORM ACTION="" METHOD="" name="DetailBlock"><INPUT TYPE="hidden" NAME="p1" VALUE="78642"><INPUT TYPE="hidden" NAME="ps" VALUE="61160224"> <INPUT TYPE="checkbox" NAME="p_dmmy_box" VALUE="123"  onClick="click_checkbox(this.checked,0)"></FORM><FORM ACTION="/dbprop/nextpage.sbmt" METHOD="POST" onsubmit="return validate();"><INPUT TYPE="submit" VALUE=" Submit " TITLE="Submit" class="button" onmouseover="this.className='hoverbutton'" onmouseout="this.className='button'" onclick=DoTheCheck(this.form)></FORM></BODY></HTML>

Note: test.load is a stored procedure, which insert data in to temp table. And this should be execute in background only(Could not redirect from current page) ThanksHb venki

Edited by hb_venkatesh
Link to comment
Share on other sites

The document object does not have a location property, the window object does. You need to use window.location.href. If you want it to send the request without loading a new page then you need to use ajax for that. The code you have will redirect the user to the first URL in the loop, it's going to stop looping the first time it redirects the user.

Link to comment
Share on other sites

Thanks for ur quick response...Im not a web developer, but i need this functionality through PSP(Procedure Server Page) program using oracle HTP package. I tried with " window.location.href" also, but i dnt get any result on this.Can you please give me small sample script to achieve this logic for your own way!!! Note: for loop will iterate more than one time, so every iteration i need to execute that href link... Thankshb Venki

Link to comment
Share on other sites

If it's not working at all then I would check your error console. That code should redirect to the first URL it finds during the first loop. After that, since it has redirected, the code stops and it's not going to continue looping. It's just going to redirect to the URL from the first time through the loop. If it's not even doing that then it sounds like you have an error that is stopping the code from running. Otherwise, this might be the problem: for (i=0; i<document.forms["DetailBlock"].p_dmmy_box.length; i++) document.forms["DetailBlock"].p_dmmy_box is a checkbox, which does not have a length property. It's not a list of checkboxes, it's a single checkbox. Are you saying that you see the alert message that tells you the checkbox is selected?

Im not a web developer
Well, that might be a problem. I can't do your work for you. Why isn't a web developer working on this?
Link to comment
Share on other sites

Hi, Above part of script is working fine, alert message will show which checkbox has selected in the form.But when i comment/remove the alert message from the for loop. No action are happening and it will redirect to the "nextpage.sbmt" page with out any console error. I guess there is no waiting time to execute the href link in js. so i tried "setTimeout" but it is also not working without alert popup. Thanks hb Venki

Link to comment
Share on other sites

The reason it's doing that is because it is submitting the form. The reason you're code wasn't redirecting to test.load is because it doesn't do anything when you change document.location.href (in fact, it's a runtime error because document.location is undefined, and you're not allowed to access a property of something that is undefined). The alert doesn't make anything work, it just pauses Javascript until you click OK. I don't know if you left out code from your example, but you're calling a function called validate when the form submits, but that function doesn't exist in your code. It doesn't look like you need that form though, if the only thing in the form is a submit button to run some Javascript code then you can just use a regular link or button to run that code instead. That way you avoid the form submitting. Regardless, it sounds like you want to send multiple requests to test.load when they click the button without changing the page, and for that you need to use ajax to send the requests: http://www.w3schools.com/ajax/default.asp

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...