Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 2 1 2 LastLast
Results 1 to 10 of 14
  1. #1
    Member Christophe's Avatar
    Join Date
    Jul 2002
    Location
    New England
    Posts
    51
    Member #
    167
    Trying to get the hang of a little JS. I want to start making "new window" links using JS, without referencing any <head> section script. Well I got one to work without specifying size of the new windo---but as soon as I specify size, doesn't seem to work.

    What am I doing wrong please? Thank you anyone.

    <a href="#" onClick="window.open('http://all-web-designers.com'); return false;">This one works</a>.

    <a href="#" onClick="window.open('http://all-web-designers.com","","height=100,width=100"); return false;">This one doesn't work</a>.

    p.s. - also would like to know, is there any js for just an ordinary link, not a new window?

    (Also will gladly pay $3 funny money to anyone who answers both questions - plus, tells me how to pay the $3...)

  2.  

  3. #2
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    The problem with the second one is that you're using quotes in your javascript AND in your HTML... you can't do that. Try this:
    Code:
    <a href="#" onClick="window.open('http://all-web-designers.com','','height=100,width=100'); return false;">
        This one should work</a>.
    For your second question:
    Code:
    <a href="#" onClick="window.location.href = 'http://all-web-designers.com';">
        This one will change the current page location</a>.

  4. #3
    Member Christophe's Avatar
    Join Date
    Jul 2002
    Location
    New England
    Posts
    51
    Member #
    167
    Thank you, Transio! I saw it done both ways in tutorials--(') vs. (")--so I thought it didn't matter. Welllll.... that answers the important question!

    If anyone has the time, here are some minor questions:

    • Even though this works:
      <a href="#" onClick="window.open("http://webdesignforums.net"); return false;">This one works</a>.

      ......But I assume it would be "safer" to do this?
      <a href="#" onClick="window.open('http://webdesignforums.net'); return false;">Maybe this one works better</a>.
    • The "sized" window as Transio suggested, has no "resize" button! It is not usable. Of course I can look up how to specify this option---also other options---but I don't like to do this. What I would like is for the "sized" window to have "whatever" option (address bar, menu bar, etc) is already defaulted---same as happens with the "unsized" new window or the old-fashioned target="_blank". Is there a simple way to do this---without specifying every single option? (If I specify every options---the person might or might not want those options! So I really prefer to allow the "default" options.)
    • Also I am a bit disappointed that the "size" for the window is followed rigidly. The new window does not reduce to the screen even if it is bigger than the screen. I would prefer to specify height="50%" and width="100%." Then size will adjust nicely. Is there any way to do this?
    • This "same window" link does not work from a page saved on my desktop:

      <a href="#" onClick="window.location.href = 'http://webdesignforums.net';">same window</a>.

      I have read somewhere that this is because---well I am not sure why it is because---but anyway this is a problem that can be and should be accounted for in the code. Otherwise if anyone "works offline" or from a "cache" or something, the link might not work. Well I will be surfing for that article again---meanwhile if someone knows the answer, please tell.
    • How do I send "Turtle Dollars"? I even did a "search" here and still I don't quite get it... Thank you!

  5. #4
    Member Christophe's Avatar
    Join Date
    Jul 2002
    Location
    New England
    Posts
    51
    Member #
    167
    Why the "window.location" link does not work from my desktop (maybe)

    Well I found that article that I referred to:

    http://www.javascriptkit.com/javatutors/links3.shtml

    It says something about adding "+window.location" into a <script> code. He is talking about a code to "view source" so it might not apply to this. However this seems to explain why a link might not work from a desktop.

    Due to the fact that a html document can be viewed both online and offline (downloaded onto a harddrive), hard coding the complete path of a file to view source will produce a bad link in one of the two cases above...
    Well it is not clear to me exactly what he means, or how to apply it to a normal link. It just "sounds like" it refers to this problem.

    So if anybody can tell me how to code a window.location link to work offline, I would be interested...

  6. #5
    Senior Member james's Avatar
    Join Date
    May 2003
    Location
    Melbourne, AUSTRALIA
    Posts
    364
    Member #
    1352
    Originally posted by Christophe
    Even though this works:
    <a href="#" onClick="window.open('http://webdesignforums.net"); return false;">This one works</a>.

    ......But I assume it would be "safer" to do this?
    <a href="#" onClick="window.open('http://webdesignforums.net'); return false;">Maybe this one works better</a>.
    Hey, they're the same! You might also want to try...

    <a href="http://webdesignforums.net" target="_new" onClick="window.open('http://webdesignforums.net'); return false;">This would work even if JS is disabled</a>.

    And...

    <a href="javascript:window.open('http://webdesignforums.net');">Same as the others</a>

    ... is more direct.
    James H
    Home Page · Mars Page · www.fihsf1.net (formerly www·fihs·net)

  7. #6
    Member Christophe's Avatar
    Join Date
    Jul 2002
    Location
    New England
    Posts
    51
    Member #
    167
    Thank you James. I am sure the first one you give works---it is just JS "added" to HTML. That is good to know, but I am trying to move a bit more towards full JS.

    Well I uploaded the following to my site and tried them online. Here is the code exactly as cut and pasted to my site, and what happens using IE-6.0.

    <a href="#" onClick="window.location.href = 'http://webdesignforums.net';">This one will change the current page location</a>.
    This does not work from desktop, but does "sort of" work online. First it adds # at the end of the current URL in the address bar---then the current page is reloaded---then it moves to the new URL. Sort of like "page forwarding." This seems a bit indirect.

    --I tried the above "working offline" and it still works. So working from "cache" is no problem, but somehow from "saved to desktop" is no good. I.e., when starting from C:\My Documents\index.shtml -- this will not work.

    --I would like to know whether or not this type of link can be followed by search spiders? I would guess not. This is not a problem, but I just would like to be aware about it.

    <a href="java script:window.open('http://webdesignforums.net');">Same as the others</a>
    This one looks good---I would like to use something like this---but so far it does not work, either online or offline! Here is what I get in the address bar (when clicking from the link using the above code and posted on "all-web-designers.com"):

    http://all-web-designers.com/java%20script:window.open('http://webdesignforums.net');

  8. #7
    Senior Member james's Avatar
    Join Date
    May 2003
    Location
    Melbourne, AUSTRALIA
    Posts
    364
    Member #
    1352
    <a href="#" onClick="window.location.href = 'http://webdesignforums.net';">This one will change the current page location</a>.
    This does not work from desktop, but does "sort of" work online. First it adds # at the end of the current URL in the address bar---then the current page is reloaded---then it moves to the new URL.
    You need to add the return false; to the end of the onClick property. What happens is (I'm pretty sure): When the link is clicked, the onClick code is executed. If this returns false, nothing else is done. If, however, it returns true (the default), the click proceeds as normal, i.e., the link is followed.
    So, I think what is happening is that the browser is told to navigate to wdf.net, then to # which is interpreted as the current page (see http://www.w3.org/TR/REC-html40/struct/links.html. It finds # first because the page is already loaded, then the remote link. Either that or the href is loaded first, then the onClick code is executed, but I don't think so...

    <a href="java script:window.open('http://webdesignforums.net');">Same as the others</a>
    This one looks good---I would like to use something like this---but so far it does not work, either online or offline
    Take out the space between 'java' and 'script'. The space is added automatically when you post here. Hence the '%20' in the address, the 'escaped' representation of a space char.

    I tested the link above, and it works (without space), but then the page with the link on it changes to [ Object ]. So I figured that the function window.open() returns a pointer to the new window, so you can manipulate it later, and this is what [ Object ] is. So to stop this from happening, You can use:

    <a href="javascript:var dummy = window.open('http://webdesignforums.net');">Same as the others</a>

    Or, preferably,
    Code:
    <script language="JavaScript">
    var PopupWin;
    function OpenPopup() {
      PopupWin = window.open('http://webdesignforums.net');
    }
    </script>
    ....
    <a href="java script:OpenPopup()">Proper JS Implementation!</a>
    <a href="javascript:PopupWin.close()">Close it.</a>
    ....
    (Tested IE6)
    The script defines a global variable PopupWin, and a function OpenPopup() which opens the new window and saves a pointer to the new window in PopupWin.
    The first link calls the function.
    The second link calls one of PopupWin's functions, namely close(). Try it! Another useful property is PopupWin.location.href. (PopupWin has all the functions that window has.)

    And I'm spent.
    James H
    Home Page · Mars Page · www.fihsf1.net (formerly www·fihs·net)

  9. #8
    Senior Member james's Avatar
    Join Date
    May 2003
    Location
    Melbourne, AUSTRALIA
    Posts
    364
    Member #
    1352
    Originally posted by Christophe [*] The "sized" window as Transio suggested, has no "resize" button! It is not usable. Of course I can look up how to specify this option---also other options---but I don't like to do this. What I would like is for the "sized" window to have "whatever" option (address bar, menu bar, etc) is already defaulted---same as happens with the "unsized" new window or the old-fashioned target="_blank". Is there a simple way to do this---without specifying every single option? (If I specify every options---the person might or might not want those options! So I really prefer to allow the "default" options.)
    Code:
    window.open('link.html', 'WindowName', 
      'toolbar=no, directories=no, location=no, 
      status=yes, menubar=no, resizable=yes, scrollbars=no, 
      width=300, height=200');
    http://www.mcli.dist.maricopa.edu/tut/tut27c.html

    Also I am a bit disappointed that the "size" for the window is followed rigidly. The new window does not reduce to the screen even if it is bigger than the screen. I would prefer to specify height="50%" and width="100%." Then size will adjust nicely. Is there any way to do this?
    No-can-do ... (?)
    James H
    Home Page · Mars Page · www.fihsf1.net (formerly www·fihs·net)

  10. #9
    Senior Member james's Avatar
    Join Date
    May 2003
    Location
    Melbourne, AUSTRALIA
    Posts
    364
    Member #
    1352
    This has all the code...
    James H
    Home Page · Mars Page · www.fihsf1.net (formerly www·fihs·net)

  11. #10
    Member Christophe's Avatar
    Join Date
    Jul 2002
    Location
    New England
    Posts
    51
    Member #
    167
    Thank you James! This looks like what I need.

    Boy, was I getting confused. Left out the "return false" etc.

    But I am glad now to "understand" better the reason for the "return false" etc. And maybe this is not entirely my mistake. I think Transio also made the mistakes here?

    <a href="#" onClick="window.location.href = 'http://all-web-designers.com';">This one will change the current page location</a>.

    Correct me again if wrong, but:

    window.location.href = 'http: -- this is wrong (?)

    window.location('http: -- this is right.

    And if so, then also:

    com';"> -- this is wrong

    com'); return false;"> -- this is right

    (Well I think James has said this already but just to be clear... Also, I will need to read the messages from James several times... looks really thorough... Thank you again, James.)


Page 1 of 2 1 2 LastLast

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 09:30 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2020 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com