/    Sign up×
Community /Pin to ProfileBookmark

redirection problem

I am new to Javascript and would really appreciate some help with a page redirection problem. I’ve searched the Web but have been unable to find a solution.

The build_url function examines the form contents and builds a URL appropriately. I didn’t think I should have any action since the function will redirect the page. Here is the opening form tag:

<FORM name=”question_info” onSubmit=”build_url(false)”>

I also have a drop-down menu in the form such that when an item is selected, the url is also built and the page is redirected (since the form changes).

The opening select tag looks like:

<SELECT name=”type” ONCHANGE=”build_url(true)”>

The last couple lines of the function looks like:

alert(str);
window.location = str;

When I use the drop down menu, the page is redirected. When I submit the form, this function is called. The right URL is displayed through the alert() function but the page is not redirected.

Thank you for any help!
Paul

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisFeb 19.2004 — It is a little hard to help you without some code to look at. Did you read the message posting guidelines? http://forums.webdeveloper.com/showthread.php?s=&threadid=16096
Copy linkTweet thisAlerts:
@icebreakerauthorFeb 19.2004 — I appreciate your comment as it might explain the low number of responses. However, I've tried to post only the pertinent information.

I'm wondering if my opening form tag syntax isn't right. Maybe, my page redirection isn't right. I've thought that perhaps the form variable somehow affects the window scope and prevents the redirection to a correcly created URL.

The actual code of the function doesn't seem important but here's more detail about what it does. The function builds a URL with several get variables. I can't just submit the form with a regular "get" method since for my textboxes - I need to replace the n with a n so the php script that processes the form can separate each line. There might be a better way but this made the most sense to me. ?

Thanks for any ideas!
Copy linkTweet thisAlerts:
@gil_davisFeb 20.2004 — Since the only difference between your two function calls is the parameter, and since one call works while the other call does not, then obviously there is a flaw in your code that is only apparent when the value "false" is passed to the function.

Since you are convinced that there is nothing pertinent in your code, and you have not bothered to post the function in question, I don't see how you can expect any sensible ideas.
Copy linkTweet thisAlerts:
@icebreakerauthorFeb 20.2004 — I may very well be wrong about the relevance of the function code to my problem. Here it is:

[CODE]<script language="JavaScript" type="text/javascript">

function escape_newline(text)
{
while (text.indexOf("n") > -1)
text = text.replace("n","\n");

return text;
}

// called when type is changed or form is submitted
function build_url(change_type)
{
var x=document.question_info;
var str = "http://10.10.10.99/~lewis/steelhead/survey/build_question.php?";

if (change_type)
str += "change_type=true&";
var end = x.elements.length;

for (var i=0; i< end; i++)
{
var ele_value = x.elements[i].value;
if (x.elements[i].name == "row_choices" || x.elements[i].name == "column_choices")
ele_value = escape_newline(ele_value)

if (i== (end-1)) // assuming submit is last input field
str += x.elements[i].name + "=" + ele_value;
else
str += x.elements[i].name + "=" + ele_value + "&";
}

alert(str);
window.location = str;
}

</script>[/CODE]

When I copy the alert message into my browser address bar, the correct URL is loaded. This is true both when change_type is true and when it is false. However, when it is false, the page is not redirected. The browser appears to simply reload the form page.

Sample URL when change_type is true:

http://10.10.10.99/~lewis/steelhead/survey/build_question.php?change_type=true&type=7&modify_mode=edit&page_num=1&question_num=1&text=Question[/url] 1&row_choices=choice 1nchoice 2nchoice 3&column_choices=Col 1nCol 2nCol 3&submit_button=Update


Sample URL when change_type is false:

http://10.10.10.99/~lewis/steelhead/survey/build_question.php?type=7&modify_mode=edit&page_num=1&question_num=1&text=Question[/url] 1&row_choices=choice 1nchoice 2nchoice 3&column_choices=Col 1nCol 2nCol 3&submit_button=Update

Thanks for your help.
Copy linkTweet thisAlerts:
@mrchipps69Feb 20.2004 — try using window.location.href instead of just window.location that may be the only problem with it.

btw you may also want to have the function return false, at least when the form is submitted, I may be wrong but if it loads the form again whenever the form is submitted you may not get the page you want if that happens after the href change.. but if you return false it will cancel the submitting process just after you change the href value...
Copy linkTweet thisAlerts:
@icebreakerauthorFeb 20.2004 — return false did the trick. Thanks!
×

Success!

Help @icebreaker spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...