"Donna Hawkins" <NO****@hotmail.com> wrote in message
news:3f**************@News.CIS.DFN.DE...
I want to use javascript to redirect to a URL which has
been passed as a variable (in php).
I have searched but cannot find any solution.
I think this code is a basic redirect:
<script type="text/javascript">
window.location = "http://www.someserver.com/somePage.html";
</script>
That is a redirect, in the sense that it will navigate the window/frame
to the URL assigned to the location object. It will only work if the
client has JavaScript enabled (and sometimes not even then).
HTTP has its own redirect method and server-side scripting languages
should provide a mechanism to use it directly. I have a limited
familiarity with PHP but in JSP the command:-
response.sendRedirect("http://example.com/examplePage.html");
- can be used to (HTTP) redirect the browser. I would be very surprised
if PHP did not provide a similar ability, and using HTTP to redirect
avoids the dependence on client-side scripting.
It is also possible to forward on the server so the client gets what it
thinks is a response to its original request but that response is
generated by a different resource to the one that the original request
was addressed to.
However, I need the URL in this code to be a variable.
I would be grateful if anyone could tell me how to make
this code handle a variable URL ...
<snip>
You have not been clear about where the variable is. Remember the
separation between PHP which executes on the server and generates a
response for the browsers and the JavaScript code included in (or
referenced by) that response, which is executed on the client. Variables
in PHP are in a different environment to JavaScript variables and they
have no direct access to each other.
To assign a JavaScript variable to the location object is as simple as
using the identifier for the variable where the string literal of the
URL appears in the script above.
A PHP variable can effectively be made available to the client-side
script included in the response by writing its value into the generated
JavaScript source code in the same way as the value of a PHP variable
can be written into the generated HTML. I don’t know the PHP equivalent
to the JSP/ASP expression tag (<%= [expression] %>) but there will be
one (and I believe ASP style scriptlet tags can be made available in
PHP). Inserting the value of a server-side Java (String) variable
(called "variableName" in this example) into JavaScript source code
generated in a JSP would be:-
<script type="text/javascript">
location = "<%= variableName %>";
</script>
Note that in order for the value of the variable to be treated as a
string literal in the resulting JavaScript source code the expression
tag is surrounded with quote marks. It is also important to consider
what the value being written into the response may contain, double
quotes and carriage returns would render the resulting JavaScript source
code syntactically incorrect and some characters may need to be escaped
when used in a string literal. That shouldn't be a problem if the string
is a URL.
Richard.