Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > How to navigate to new URL without new instance of browser

Reply
Thread Tools

How to navigate to new URL without new instance of browser

 
 
Paul E. Schoen
Guest
Posts: n/a
 
      09-22-2009
This is probably stupid simple, but I just want to run a script that
navigates from the web page that has just been opened to another web page
with a different URL. I would also like to have an alert appear for a short
time stating that another web page is being opened. Here is my complete
script (in links.htm):

<SCRIPT LANGUAGE="JScript">
<!--
function Load_bsnlinks()
{
open("bsnlinks.htm", "NewWindow");
}
alert("Transferring to bsnlinks.htm");
setTimeout(Load_bsnlinks, 3000);
<!-- End Script -->
</SCRIPT>

If I use the alert, it seems to pause execution until the user clicks its
OK button. Then the "links.htm" page loads, and after a short delay a new
instance of the browser appears with the "bsnlinks.htm". But I want it to
load in the original window, as if someone clicked on a link.

Sorry if this is trivial, but I checked the FAQ and found only the open()
function.

Thanks,

Paul


 
Reply With Quote
 
 
 
 
SAM
Guest
Posts: n/a
 
      09-22-2009
Le 9/22/09 8:10 PM, Paul E. Schoen a écrit :
> This is probably stupid simple, but I just want to run a script that
> navigates from the web page that has just been opened to another web page
> with a different URL. I would also like to have an alert appear for a short
> time stating that another web page is being opened. Here is my complete
> script (in links.htm):
>
> <SCRIPT LANGUAGE="JScript">
> <!--
> function Load_bsnlinks()
> {
> open("bsnlinks.htm", "NewWindow");
> }
> alert("Transferring to bsnlinks.htm");
> setTimeout(Load_bsnlinks, 3000);
> <!-- End Script -->
> </SCRIPT>
>
> If I use the alert, it seems to pause execution until the user clicks its
> OK button. Then the "links.htm" page loads, and after a short delay a new
> instance of the browser appears with the "bsnlinks.htm". But I want it to
> load in the original window, as if someone clicked on a link.
>
> Sorry if this is trivial, but I checked the FAQ and found only the open()
> function.


window.open() ! ( document.open() exists too)

location = 'myNewOtherFile.htm';
or (but I don't know the difference between both) :
location.href = 'myNewOtherFile.htm';



===== Example 1:

<script type=text/javascript">
function Load_bsnlinks(theFile)
{
if(confirm('Do you accept the transfer to : '+theFile+'?'))
setTimeout('location='+theFile, 1000);
else alert('OK. Transfer aborted.');
}
</script>
<button onclick="Load_bsnlinks('bsnlinks.htm')">
bsn links
</button>


===== Example 2:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script type=text/javascript">
if(confirm('Do you accept the transfer to : '+theFile+'?'))
location = theFile;
else alert('OK. Transfer aborted.');
</script>
<title>Untitled</title>
</head>
<body>
<h1>hello</h1>
<noscript>
<p>You probably haven't your JavaScript enabled,<br>
please click that <a href="bsnlinks.htm">link</a></p>
</noscript>
<p>You've chosen to stay here... and now what can we do ?</p>
</body>
</html>



===== Example 3 (without JS) :

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="refresh" content="3;bsnlinks.htm">
<title>Untitled</title>
</head>
<body>
<h1>refresh</h1>
<p>You'll been soon redirected to : <tt>bsnlinks.htm</tt></p>
<p>You may click that <a href="bsnlinks.htm">link</a></p>
</body>
</html>


--
sm
 
Reply With Quote
 
 
 
 
Paul E. Schoen
Guest
Posts: n/a
 
      09-23-2009
Thanks for all the alternative ways to automatically forward to another web
page. I prefer the method using only HTML, so that Javascript need not be
enabled. But the example provided just refreshed the original page at the
specified interval. I found a more detailed example here:
http://www.htmlcodetutorial.com/docu...tagsupp_4.html

Here is the final version that works for me (Note that URL= is required):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="refresh" content="5; URL=bsnlinks.htm">
<title>links.htm (old BSN links)</title>
</head>
<body>
<h1 align="center"><B><FONT COLOR="#ff0000">This is the old links.htm
page</font></h1>
<p align='center'>You'll been soon redirected to :
<tt>bsnlinks.htm</tt></p>
<p align='center'>You may click: <a href="bsnlinks.htm">Go To New BSN
Links</a></p>
</body>
</html>

Here is a link to the actual web page:
http://www.smart.net/~pstech/bsn/links.htm

Paul


 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      09-23-2009
Paul E. Schoen wrote on 22 sep 2009 in comp.lang.javascript:

> This is probably stupid simple, but I just want to run a script that
> navigates from the web page that has just been opened to another web
> page with a different URL. I would also like to have an alert appear
> for a short time stating that another web page is being opened. Here
> is my complete script (in links.htm):
>
> <SCRIPT LANGUAGE="JScript">


this 'language' is last century's coding,
and JScript is only the M$ name.

use

<script type='text/javascript'>


> <!--


same objection, do not use <--,
there is no modern browser that requires it

> function Load_bsnlinks()
> {


Crockford "Style matters" explains that having the open { on the same
line is safer.

function Load_bsnlinks() {


> open("bsnlinks.htm", "NewWindow");


windows.open() is slightly safer.

"NewWindow" does not open a new window,
but a window named 'NewWindow'.

If you want to open a new window,
use '_blank' or simply leave
the target parameter empty or absent:

windows.open('bsnlinks.htm');


> }


> alert("Transferring to bsnlinks.htm");


javascript, being single threaded waits till the alert() is answered.

> setTimeout(Load_bsnlinks, 3000);


so the timout is not sensible.

> <!-- End Script -->


No need for that, see above

> </SCRIPT>


uppercase is alowesd, but why not echo the opening <script

> If I use the alert, it seems to pause execution until the user clicks
> its OK button.


acording to specs.

> Then the "links.htm" page loads, and after a short
> delay a new instance of the browser appears with the "bsnlinks.htm".
> But I want it to load in the original window, as if someone clicked on
> a link.


use:

window.location.href = 'bsnlinks.htm';


> Sorry if this is trivial, but I checked the FAQ and found only the
> open() function.


It helps if you read the specs, whose urls are offered in the FAQ.

Do not use old and inappropriate scripts,
unless you thouroughly understand them
and alter them to the 21th century and to your needs.

Try:

=============================================
<b>Transferring to bsnlinks.htm</b>
<script type='text/javascript'>
window.setTimeout("window.location.href='bsnlinks. htm'",3000);
</script>
=============================================

Or do not use javascript at all:

=============================================
<meta http-equiv="refresh" content="3;url=bsnlinks.htm">
<b>Transferring to bsnlinks.htm</b>
=============================================



--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      09-23-2009
Stefan Weiss wrote:
> On 23/09/09 00:34, SAM wrote:
>> Le 9/22/09 8:10 PM, Paul E. Schoen a écrit :
>>> This is probably stupid simple, but I just want to run a script that
>>> navigates from the web page that has just been opened to another web page
>>> with a different URL. I would also like to have an alert appear for a short
>>> time stating that another web page is being opened. Here is my complete
>>> script (in links.htm):
>>>
>>> <SCRIPT LANGUAGE="JScript">
>>> <!--
>>> function Load_bsnlinks()
>>> {
>>> open("bsnlinks.htm", "NewWindow");
>>> }
>>> alert("Transferring to bsnlinks.htm");
>>> setTimeout(Load_bsnlinks, 3000);
>>> <!-- End Script -->
>>> </SCRIPT>

>
> window.open() will always (try to) open a new window. If you want to
> load a new document in the current window/tab, use the |window.location|
> object.


Only if that does not break (backward/forward) navigation.

>> location = 'myNewOtherFile.htm';
>> or (but I don't know the difference between both) :
>> location.href = 'myNewOtherFile.htm';

>
> Browsers support assigning a URL string to the window.location object
> directly; it has the same effect as assigning to its href property. The
> latter is better style, IMO. The former was probably intended as a
> shortcut, and can't be dropped now because it's too widely used. Anyway,
> both assignments have "magical" side effects (=load a new document).


The difference is, or at least was, that the `href' property of Location
instances was/is "tainted", while the `location' property of Window
instances was/is not. That means that the `location' property was/is not
subject to the Same Origin Policy, thus could/can be used for accesses that
the Policy would normally forbid.

<http://docs.sun.com/source/816-6409-10/sec.htm#1021266>

(Note: Nowadays [since JavaScript 1.4, and never in JScript or other
implementations] those host objects are no longer part of the programming
language but of the DOM implementation. However, that tainting was removed
in JavaScript 1.2 is incorrect; instead, it appears to have been enabled by
default for at least the Gecko DOM.)

>> <script type="text/javascript">

> [typo fixed]
>
> @Paul: This is the correct way to open a script tag.


It is the _start tag_ of a script _element_. The element is _started_, not
opened, and _ended_ (with an end tag), not closed. (When I started Web
development, I did that one wrong, too. Some kind people pointed me to the
Specification then.)

<http://www.w3.org/TR/REC-html40/intro/sgmltut.html>

> The language attribute is obsolete,


No, it is _deprecated_ in HTML, which is different (declared for
Transitional variants, not so for Strict variants). If it was *obsolete*
already, the current HTML Specification (4.01) would not define it at all.
IOW: the terms "deprecated" and "obsolete" must be set in relation to a
specific version of a specific markup language.

> language="JScript" will only work in Internet Explorer


No, it is going to work in all MSHTML-based browsers, but probably not many
others (so not in Gecko-based ones, like Firefox/Iceweasel).

> (an unnecessary restriction in your case),


Yes, it is at least unwise and of course invalid without the `type' attribute.

> and element names are usually written in lower-case (today).


,-<http://www.flightlab.com/~joe/sgml/faq-not.txt>
|
| Q. How do I get the current element name in XSLT?
|
| A. That's element *type* name, dammit!

It is not a requirement as element _type names_ are case-insensitive in
HTML, but it helps compression (e.g., with `Transfer-Encoding: gzip', or on
a compressed server filesystem) as lowercase letters are prevalent in
Latin-based texts, thus usually compress better than equivalent uppercase
letters. It also helps migration to XHTML where lowercase element type
names and attribute names are required (which is what you might mean by
referring to "today"; keep in mind though that XHTML support is still not
prevalent in user agents -- most notably IE/MSHTML does not support it --,
and that XHTML 2 development was recently reduced/suspended by W3C in favor
of development of HTML 5.)

> @Paul: A (not so minor) nitpick: lose the HTML comments inside your
> script. The way you use them makes your HTML invalid,


No, evidently it doesn't. The reason is that the content of the *HTML*
`script' element is declared CDATA, not PCDATA. So it is not parsed (except
of the `</' [ETAGO delimiter] which ends the element content). Just run it
through <http://validator.w3.org/> to see for yourself.

It would be syntactically invalid script and XHTML code, of course.

> and the times where scripts had to be hidden from the browser are long gone.


Definitely. HTML 3.2, which introduced the `script' element and mandates
that user agents should hide the element's content dates back to 1997-01 CE.

> <script type="text/javascript">
> function loadBSNLinks () {
> document.getElementById("linkMsg").style.display = "block";
> window.setTimeout("window.location.href = 'bsnlinks.htm'", 1000);
> }
> </script>
>
> <button onclick="loadBSNLinks()">try it</button>
>
> <div id="linkMsg" style="display:none">
> Transferring to bsnlinks.htm
> </div>
>
>
> Set the styles for the linkMsg element so that it appears as a pseudo-alert.


The whole idea of the OP is utter nonsense already, and should not be
supported by posting bogus solutions. There has been no real problem
presented to be solved to begin with, and the Web is *haunted* by the
results of such inept attempts at seemingly improving usability, thereby
effectively reducing it, already.

In this case, there is no good reason to force the user to wait one second
before navigating elsewhere. Do you really think they bought a fast
computer to let it show someone's wait message?


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      09-23-2009
Stefan Weiss wrote:
> On 23/09/09 11:51, Thomas 'PointedEars' Lahn wrote:
>> Stefan Weiss wrote:
>>> language="JScript" will only work in Internet Explorer

>> No, it is going to work in all MSHTML-based browsers, but probably not many
>> others (so not in Gecko-based ones, like Firefox/Iceweasel).

>
> AFAIK, JScript lives in jscript.dll, not mshtml.dll.


Regardless, the layout engine's markup parser is required in order to
recognize code that should be passed to a script engine. And I have yet to
see a distribution of MSHTML that comes without JScript support. While that
should go without saying, consider language="VBScript" and think about what
would be necessary in order to invoke vbscript.dll in a Web browser
(component) then.

>>> Set the styles for the linkMsg element so that it appears as a pseudo-alert.

>> The whole idea of the OP is utter nonsense already, and should not be
>> supported by posting bogus solutions. There has been no real problem
>> presented to be solved to begin with, and the Web is *haunted* by the
>> results of such inept attempts at seemingly improving usability, thereby
>> effectively reducing it, already.
>>
>> In this case, there is no good reason to force the user to wait one second
>> before navigating elsewhere. Do you really think they bought a fast
>> computer to let it show someone's wait message?

>
> We don't know why he wanted to display the message. Maybe there's a good
> reason for it, maybe there isn't.


The OP, his history here, his script-kiddy-ish wording and source code is a
strong indication to some that there is not a good reason, just a desire to
do something fancy.

> This group is about JavaScript, not UI design;


This newsgroup is about providing the best solutions possible when the
problem is related to client-side scripting, and about discussing possible
mistakes when looking for one. And, if I may say so, as a newbie here you
are not really in a position to make an informed judgment about what is and
is not on-topic here. Regardless, the FAQ and archives provide enough hints.

> I merely provided a working version of his earlier attempt.


For suitable values of "working".


PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrman
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      09-23-2009
Thomas 'PointedEars' Lahn wrote:
> Stefan Weiss wrote:
>> This group is about JavaScript, not UI design;

>
> This newsgroup is about providing the best solutions possible when the
> problem is related to client-side scripting,


JFTR: ... and server-side ECMAScript-based scripting of course, but
ECMAScript implementations are undoubtedly prevalent on the client side.

> and about discussing possible mistakes when looking for one. And, if I
> may say so, as a newbie here you are not really in a position to make an
> informed judgment about what is and is not on-topic here. Regardless,
> the FAQ and archives provide enough hints.
> [...]



PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
 
Reply With Quote
 
Paul E. Schoen
Guest
Posts: n/a
 
      09-23-2009

"Thomas 'PointedEars' Lahn" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Stefan Weiss wrote:
>> On 23/09/09 11:51, Thomas 'PointedEars' Lahn wrote:


>>> The whole idea of the OP is utter nonsense already, and should not be
>>> supported by posting bogus solutions. There has been no real problem
>>> presented to be solved to begin with, and the Web is *haunted* by the
>>> results of such inept attempts at seemingly improving usability,
>>> thereby
>>> effectively reducing it, already.
>>>
>>> In this case, there is no good reason to force the user to wait one
>>> second
>>> before navigating elsewhere. Do you really think they bought a fast
>>> computer to let it show someone's wait message?

>>
>> We don't know why he wanted to display the message. Maybe there's a good
>> reason for it, maybe there isn't.

>
> The OP, his history here, his script-kiddy-ish wording and source code is
> a
> strong indication to some that there is not a good reason, just a desire
> to
> do something fancy.


I am admittedly a newbie in JavaScript and it is only a very small part of
the skill set I use for my professional and personal needs. My purpose for
this was to fix a broken link that someone apparently has on their website,
which points to my old links.htm page rather than the newer bsnlinks.htm.
So I wanted to alert the user to the fact that this link was no longer
valid, and then forward them to the correct page. If I did not include a
delay, the new web page would appear almost immediately, and they would not
know that their link was broken. I did not know that this could be done
even better using just the HTML META refresh method, which does not require
JavaScript at all.

I am grateful to those who pointed me in the right direction so I could
provide the best solution. I am not looking to do anything fancy, although
it is good to have the knowledge to do so. But I do feel that it is
disrespectful and supercilious of you to make the assumptions and
accusations above. You may be an expert in this field, but your social
skills could use a little polish.

Paul


 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      09-25-2009
Paul E. Schoen wrote:
> I am admittedly a newbie in JavaScript and it is only a very small part of
> the skill set I use for my professional and personal needs. My purpose for
> this was to fix a broken link that someone apparently has on their website,
> which points to my old links.htm page rather than the newer bsnlinks.htm.


So you set up a *server-side* redirect. You definitely do not use scripting
or HTML or any other client-side means for that.

> So I wanted to alert the user to the fact that this link was no longer
> valid, and then forward them to the correct page. If I did not include a
> delay, the new web page would appear almost immediately, and they would not
> know that their link was broken. I did not know that this could be done
> even better using just the HTML META refresh method, which does not require
> JavaScript at all.


How can it be better when it breaks the Back button

> [Rubbish snipped] You may be an expert in this field, but your social
> skills could use a little polish.


Pot, kettle, black. This is _not_ a support forum.

<http://jibbering.com/faq/#posting> pp.


I'd say "Score adjusted" if that was not already done.

PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
 
Reply With Quote
 
SAM
Guest
Posts: n/a
 
      09-25-2009
Le 9/25/09 4:08 PM, Thomas 'PointedEars' Lahn a écrit :
> Paul E. Schoen wrote:
>> I am admittedly a newbie in JavaScript and it is only a very small part of
>> the skill set I use for my professional and personal needs. My purpose for
>> this was to fix a broken link that someone apparently has on their website,
>> which points to my old links.htm page rather than the newer bsnlinks.htm.

>
> So you set up a *server-side* redirect. You definitely do not use scripting
> or HTML or any other client-side means for that.


Non, je ne suis pas d'accord ! !

I don't agree with you, even if this possibility could be studied and
used, supposing the "webmaster" is allowed to access to the server.
Does that will correct the bookmark of the visitor ?
(if he came on that no more existing page, wasn't it because he used a
wrong bookmark? I think to offer him a possibility to know that this
bookmark is wrong and to correct it is not a so bad idea).

>> So I wanted to alert the user to the fact that this link was no longer
>> valid, and then forward them to the correct page. If I did not include a
>> delay, the new web page would appear almost immediately, and they would not
>> know that their link was broken. I did not know that this could be done
>> even better using just the HTML META refresh method, which does not require
>> JavaScript at all.

>
> How can it be better when it breaks the Back button


pfffft !

The guy certainly came on this page hitting one of his bookmarks that is
out of date, what he has to do with a back button ?

Anyway I do not understand your idea of no more back button.
The back-button seems to jump over the refreshing page, is it really
serious doctor ?


>> [Rubbish snipped] You may be an expert in this field, but your social
>> skills could use a little polish.

>
> Pot, kettle, black. This is _not_ a support forum.


Yes, it can be, why not.
Or is it forbidden in a rule, or in a law, somewhere?

--
sm
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
webrequest post method and navigate to the new url guoqi zheng ASP .Net 3 05-29-2006 07:06 AM
Navigate to dynamically created URL on button click =?Utf-8?B?cmFq?= ASP .Net 0 04-12-2005 04:09 PM
How to: programatically navigate to a URL (really an anchor) Dan Hinsley ASP .Net 3 11-27-2004 06:22 PM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM



Advertisments