SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Zealot
    Join Date
    Dec 1999
    Location
    Highlands Ranch, CO
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help quick please (javascript)

    I am trying to code a link that has a viariable as the 'target'. Either _top or _blank based on what page they came to the page from.

    Here is my code:
    PHP Code:
    <!--
    var 
    ref;
    var 
    target;
    ref window.location.href;

    function 
    targetnew() {
    if (
    ref.indexOf("/ssi/frames") == -1)
    {
    document.write("target = \"_top\"");
    }
    else (
    ref.indexOf("/ssi/frames") == 1)
    {
    document.write("target = \"_blank\"");
    }}
    //--> 
    I thought I had it working with the link:

    <a href="times.asp" javascript=targetnew()>Times</a>

    But it doesn't work (I'm a newbie =). Is there a simple way to get the function to determine the target?

    Thank you =)
    Last edited by Chad J; Sep 27, 2001 at 13:02.

  2. #2
    SitePoint Zealot
    Join Date
    Dec 1999
    Location
    Highlands Ranch, CO
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just in case it helps, I need several links on the page to either have a target of "_blank" or have a target of "_top" based on how they got to this page. It does need to be javascript as well.

    Hence the reason for the "ref = window.location.href;"

    Thanks for any help!

  3. #3
    I am the night... bman's Avatar
    Join Date
    Mar 2001
    Location
    In a van... down by the river.
    Posts
    366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    maybe try this

    <a href="times.asp"
    <script language="javascript">
    targetnew()
    </script>
    >Times</a>


    keep in mind it has been awhile since i have really written any javascript..

    ---EDIT---
    nah nevermind, i dont think it is gonna work. it was worth a shot though.
    Last edited by bman; Sep 27, 2001 at 13:24.
    Some say the world will end in fire, Some say in ice.
    From what I've tasted of desire
    I hold with those who favor fire.

  4. #4
    I am the night... bman's Avatar
    Join Date
    Mar 2001
    Location
    In a van... down by the river.
    Posts
    366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, i gave it some more thought, my last post was a way off..

    Code:
    <script language="javascript">
    <!--
    var ref;
    var target;
    ref = window.location.href;
    
    function targetnew() {
    if (ref.indexOf("/ssi/frames") == -1)
    {
    target="target = '_top'";
    }
    else (ref.indexOf("/ssi/frames") == 1)
    {
    target="target = '_blank'";
    }}
    //-->
    </script>
    then for where you want the link, do this

    Code:
    <script language="javascript">
    document.write("<a href='times.asp' " + target + ">Times</a>")
    </script>
    Some say the world will end in fire, Some say in ice.
    From what I've tasted of desire
    I hold with those who favor fire.

  5. #5
    SitePoint Wizard silver trophy Jeremy W.'s Avatar
    Join Date
    Jun 2001
    Location
    Toronto, Canada
    Posts
    9,123
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Why not use ASP, since you seem to be anyways...
    Code:
    <%
    ref=Request.ServerVariables("HTTP_REFERER")
    if inStr(ref,"/ssi/frames") then
    	target="_top"
    else
    	target="_self"
    end if
    %>
    Then, you'd have the links with something like:

    <a href="times.asp" target="<%=target%>">Times</a>
    SVP Marketing, SoCast SRM
    Personal blog: Strategerize
    Twitter: @jeremywright

  6. #6
    I am the night... bman's Avatar
    Join Date
    Mar 2001
    Location
    In a van... down by the river.
    Posts
    366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i knew there had to be a better way than what i was doing.
    i swear i am going to learn asp one of these days..
    Some say the world will end in fire, Some say in ice.
    From what I've tasted of desire
    I hold with those who favor fire.

  7. #7
    SitePoint Zealot
    Join Date
    Dec 1999
    Location
    Highlands Ranch, CO
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    bman:

    is there something wrong with the code you provided? It is not working - no errors, but it just makes the links open in the same window (irregardless of the referrer).

    Jeremy:

    I can't use ASP because of the structure. The files that this code is on are 'included' files. So if I were to use asp, the code would not resolve at the server - since the page has loaded. At least I don't believe that ASP will work on included files. Let me know if I am wrong.

    Overall:

    I am still needing a solution. Please help!

  8. #8
    SitePoint Wizard silver trophy Jeremy W.'s Avatar
    Join Date
    Jun 2001
    Location
    Toronto, Canada
    Posts
    9,123
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    <!--#include file="connect.asp"-->

    Where's the problem?
    SVP Marketing, SoCast SRM
    Personal blog: Strategerize
    Twitter: @jeremywright

  9. #9
    SitePoint Zealot
    Join Date
    Dec 1999
    Location
    Highlands Ranch, CO
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Jeremy,

    I can't use server side includes, so I went with the following code:
    <%
    Set FSO = Server.CreateObject("Scripting.FileSystemObject")
    Set inFile = FSO.OpenTextFile( Server.MapPath( "/ssi/join/" & strFile & ".htm" ) )
    Response.Write inFile.ReadAll
    inFile.Close
    Set inFile = nothing
    %>

    The strFile is a variable that is passed in the URL. So the code determines what file needs to be included > opens that file > writes the code from the file into the page.

    So the code I am looking to make work (variable 'target') is in the code that is read, and written into the page. I tried making the include and .asp file, but the asp code from the include was not being resolved at the server. So I figured that it needs to be a client side function.

    Does this make sense? or am I missing something?

  10. #10
    SitePoint Zealot
    Join Date
    Dec 1999
    Location
    Highlands Ranch, CO
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok. Now I got it working - BUT, there is something that makes me wonder...

    I did a document.write on the ref.indexOf statement expecting to get either a 1 or -1.

    But I got a '-1' and a '23' (depending on who the referer is).

    Does this make sense to anyone???

    So basically the code below works:
    PHP Code:
    <script language="javascript">
    <!--
    var 
    ref;
    var 
    target;
    ref window.location.href;

    if (
    ref.indexOf("ssi/frames") == 23)
    {
    target="target = '_blank'";
    }
    else
    {
    target="target = '_top'";
    }
    //-->
    </script> 
    What kind of sense does that make?

  11. #11
    SitePoint Zealot
    Join Date
    Dec 1999
    Location
    Highlands Ranch, CO
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nevermind, I just realized that the indexOf method returns -1 or the position in the string of the match (23rd). Is there a function that returns true or false?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •