ASP variable in javascript ??

Hi,

I have a javascript that generates a menu. It's a really cool drop down menu so I don't want to change to much to that HOWEVER, The links in the javascript are static and now I want to attach a asp variable to every link. The variable (strlan) is defined on top of the page.

The URL in the javascript is "page.asp" and I want it to do something like this:

"page.asp?lan=<% strlan %>"

I understand the fact that that Js is clientside and ASP is serverside code but surely there must be a way ?

Since the variable is the same for every link on the page (It's the language the page is shown in), a possible option could be that I include some ASP code in the page that attaches the variable to every link on that page once it is clicked. This is called redirecting I think but not sure...
Ok hope someone can help !
voermanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Michel PlungjanIT ExpertCommented:
<html>
<head>
<script>
lan="<% strlan %>"
function addLang() {
   for (i=0;i<document.links.length;i++) {
      aspPos = document.links[i].href.indexOf('.asp');
      if (aspPos!=-1) {
         qPos = document.links[i].href.indexOf('.asp?');
         if (qPos !=-1) document.links[i].href+='&lan='+lan
         else document.links[i].href+='?lan='+lan
      }
   }
}
</script>
</head>
<body onLoad="addLang()">
0
Michel PlungjanIT ExpertCommented:
the test is looking to see if you have
xxxx.asp?somevar=someval
and will create
xxxx.asp?somevar=someval&lan=zz
or if you do not it will create
xxxx.asp?lan=zz

Michel
0
David H.H.LeeCommented:
voerman,
you can try solve it like this way [use server-side language in client-side]:

...
window.open('page.asp?lan='+'<%=strlan %>')
..

Regards
x_com
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

voermanAuthor Commented:
mplungjan,

It's nearly working but not quite. It's not attaching the lan thing to my links: Is it supposed to do it after I click on a link ?


<script>

lan="<% strlan %>" ,<-- Is this correct ???

function addLang() {
   for (i=0;i<document.links.length;i++) {
      aspPos = document.links[i].href.indexOf('.asp');
      if (aspPos!=-1) {
         qPos = document.links[i].href.indexOf('.asp?');
         if (qPos !=-1) document.links[i].href+='&lan='+lan
         else document.links[i].href+='?lan='+lan
      }
   }
}
</script>
</head>
<body onLoad="addLang()">
0
David H.H.LeeCommented:
voerman ,
mplungjan's code just missing something,
lan='<%=strlan %>'
0
voermanAuthor Commented:
still not working. Some info:

 I am getting the strLan from the URL with : <%strLan = Request.QueryString("lan")%>

And the whole thing is in a include file ; does that matter ?

0
David H.H.LeeCommented:
voerman,
It sure be work. Can u see the output for strLan in server-side?
<%
strLan = Request.QueryString("lan")
Response.Write(strLan)
%>
0
Michel PlungjanIT ExpertCommented:
Yeah, sorry I missed the equals in

lan="<% strlan %>"
should be

lan="<%= strlan %>"

Works fine for me:


<html>
<head>
<script>
lan="<%= strlan %>"
function addLang() {
   for (i=0;i<document.links.length;i++) {
      aspPos = document.links[i].href.indexOf('.asp');
      if (aspPos!=-1) {
         qPos = document.links[i].href.indexOf('.asp?');
         if (qPos !=-1) document.links[i].href+='&lan='+lan
         else document.links[i].href+='?lan='+lan
      }
   }
}
</script>
</head>
<body onLoad="addLang()">
<a href="link1.asp">Link1</a>
<a href="link2.asp?test=x">Link2</a>



0
voermanAuthor Commented:
Yeah I made this test page and it works ! But still not in my "real" page.

Could it be that the fact that this piece of code is in a include file ?

<%strLan = Request.QueryString("lan")%>
<html>
<head>
<script>
lan="<%= strlan %>"
function addLang() {
   for (i=0;i<document.links.length;i++) {
      aspPos = document.links[i].href.indexOf('.asp');
      if (aspPos!=-1) {
         qPos = document.links[i].href.indexOf('.asp?');
         if (qPos !=-1) document.links[i].href+='&lan='+lan
         else document.links[i].href+='?lan='+lan
      }
   }
}
</script>
</head>
<body onLoad="addLang()">
<a href="link1.asp">Link1</a>
<a href="link2.asp?test=x">Link2</a>
</body>
</html>
0
voermanAuthor Commented:
xcom, yeah I can see the output in text but not attached to a link anywhere.
0
voermanAuthor Commented:
Does this system also attach the lan= to a link generated in javascript ?
0
Michel PlungjanIT ExpertCommented:
It will add the lan to all links on the page after the page has loaded
0
Michel PlungjanIT ExpertCommented:
Show the HTML produced by the non working code, please

Michel
0
mrmcfeelyCommented:
Make sure your include is coded before the javascript in your source code.

Remember that ASP includes are inline... if you reference a variable declared in the included file before the file is included (that is, the variable reference is above the include directive in the code), you'll get an error (saying that the variable has not been declared).
0
voermanAuthor Commented:
This is the include file : top.asp


<%@ LCID=1043%>
<%strTitel = Request.QueryString("titel")%>
<%strSub = Request.QueryString("Sub")%>
<%strSubUrl = Request.QueryString("SubUrl")%>
<%strDbTable = Request.QueryString("DbTable")%>
<%strLan = Request.QueryString("lan")%>
<html>
<head>
<title</title>
<LINK REL=STYLESHEET TYPE="text/css" HREF="stylesheet/style.css">
<script>
lan="<%= strLan %>"
function addLang() {
   for (i=0;i<document.links.length;i++) {
      aspPos = document.links[i].href.indexOf('.asp');
      if (aspPos!=-1) {
         qPos = document.links[i].href.indexOf('.asp?');
         if (qPos !=-1) document.links[i].href+='&lan='+lan
         else document.links[i].href+='?lan='+lan
      }
   }
}
</script>


</head>


<body onLoad="addLang()" bgcolor="#BF1F0B" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" > 
<table width="100%" border="0">
  <tr>
    <td width="34%"><img src="art/logo.gif" width="265" height="40"></td>
    <td width="10%">&nbsp;</td>
    <td width="18%" valign="top"><font color="#FFFFFF" size="1" face="Verdana">
      <select name="select" class="area">
        <option>Kies een andere dag</option>
        <option>1 Oktober 2003</option>
        <option>2 Oktober 2003</option>
        <option>3 Oktober 2003</option>
        <option>Gisteren</option>
        <option>Vandaag</option>
      </select>
    </font></td>
    <td width="38%" align="right" valign="top"><font color="#FFFFFF" size="1" face="Verdana">
<a href="page_view.asp?titel=Wat&DbTable=wat" class="menu1">Wat</a> |
<a href="page_view.asp?titel=Hoe&DbTable=hoe" class="menu1">Hoe</a> |
<a href="page_view.asp?titel=Colofon&DbTable=colofon" class="menu1">Colofon</a> |
<a href="page_view.asp?titel=Service&DbTable=service" class="menu1">Service</a> |

<%Response.Write FormatDateTime(Now(), 1)%>
<img src="art/clearpixel.gif" width="1" height="1"></font></td>
  </tr>
</table>
<%
Dim ANRec, ANConn, strANConn
strANConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("admin/babel.mdb")
      Dim StickPos
      StickPos = 0

      Dim NormPos
      NormPos = 0
%>

Hulptaal:
<% If (strLan) = "1" Then %>
Nederlands
<% ElseIf (strLan) = "2" Then %>
Arabisch
<% ElseIf (strLan) = "3" Then %>
Persisch
<% ElseIf (strLan) = "4" Then %>
Turks
<% ElseIf (strLan) = "5" Then %>
Spaans
<% ElseIf (strLan) = "6" Then %>
Frans
<% ElseIf (strLan) = "7" Then %>
Duits
<% ElseIf (strLan) = "8" Then %>
Engels
<% End If %>
<p><script type="text/javascript" language="javascript" src="jscript/sniffer.js"></script>
  <script type="text/javascript" language="javascript1.2" src="jscript/custom.js"></script>
  <script type="text/javascript" language="javascript1.2" src="jscript/style.js"></script>
  <strong>  <font color="#FFFFFF" size="2" face="Verdana">
  <img src="art/clearpixel.gif" width="8" height="1"><font size="1">
<img src="art/arrowred.jpg" width="14" height="13" vspace="4" align="absmiddle"><a href="voorpagina.asp" class="menu1"> Voorpagina </a>
0
Michel PlungjanIT ExpertCommented:
Please show the HTML as it appears when you do VIEW-SOURCE in the BROWSER
0
voermanAuthor Commented:

<html>
<head>
<title>.:BabelExpress:. </title>
<LINK REL=STYLESHEET TYPE="text/css" HREF="stylesheet/style.css">

<script language="JavaScript">
<!--

function SymError()
{
  return true;
}

window.onerror = SymError;

//-->
</script>

<script>
lan="1"
function addLang() {
   for (i=0;i<document.links.length;i++) {
      aspPos = document.links[i].href.indexOf('.asp');
      if (aspPos!=-1) {
         qPos = document.links[i].href.indexOf('.asp?');
         if (qPos !=-1) document.links[i].href+='&lan='+lan
         else document.links[i].href+='?lan='+lan
      }
   }
}
</script>


</head>


<body onLoad="addLang()" bgcolor="#BF1F0B" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" > 
<table width="100%" border="0">
  <tr>
    <td width="34%"><img src="art/logo.gif" width="265" height="40"></td>
    <td width="10%">&nbsp;</td>
    <td width="18%" valign="top"><font color="#FFFFFF" size="1" face="Verdana">
      <select name="select" class="area">
        <option>Kies een andere dag</option>
        <option>1 Oktober 2003</option>
        <option>2 Oktober 2003</option>
        <option>3 Oktober 2003</option>
        <option>Gisteren</option>
        <option>Vandaag</option>
      </select>
    </font></td>
    <td width="38%" align="right" valign="top"><font color="#FFFFFF" size="1" face="Verdana">
<a href="page_view.asp?titel=Wat&DbTable=wat" class="menu1">Wat</a> |
<a href="page_view.asp?titel=Hoe&DbTable=hoe" class="menu1">Hoe</a> |
<a href="page_view.asp?titel=Colofon&DbTable=colofon" class="menu1">Colofon</a> |
<a href="page_view.asp?titel=Service&DbTable=service" class="menu1">Service</a> |

maandag 27 oktober 2003
<img src="art/clearpixel.gif" width="1" height="1"></font></td>
  </tr>
</table>


Hulptaal:

Nederlands

<p><script type="text/javascript" language="javascript" src="jscript/sniffer.js"></script>
  <script type="text/javascript" language="javascript1.2" src="jscript/custom.js"></script>
  <script type="text/javascript" language="javascript1.2" src="jscript/style.js"></script>
  <strong>  <font color="#FFFFFF" size="2" face="Verdana">
  <img src="art/clearpixel.gif" width="8" height="1"><font size="1">
<img src="art/arrowred.jpg" width="14" height="13" vspace="4" align="absmiddle"><a href="voorpagina.asp" class="menu1"> Voorpagina </a>


</font></strong>
<table width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="9" height="9" background="art/topleft.gif"></td>
    <td height="9" colspan="4" background="art/top.gif" bgcolor="#FFFFFF"></td>
    <td width="9" height="9" background="art/topright.gif"></td>
  </tr>
  <tr>
    <td width="9" background="art/left.gif" bgcolor="#FFFFFF"></td>
    <td width="160" align="left" valign="top" bgcolor="#FFFFFF"><img src="art/upload/Hoofdstuk1.gif" width="152" border="0"></td>
    <td valign="top" bgcolor="#FFFFFF">
<font size="5">Volgens allochtonen teveel allochtonen in ons land</font><br>
<font color="#BF1F0B" size="1">26.10.2003 | Nieuws Binnenland</font><br><font size="4">De helft van de allochtonen in Nederland vindt dat er teveel allochtonen zijn. En meer dan de helft vindt dat een wijk slechter wo...<a href="page_view.asp?normpos=0&titel=Binnenland&Sub=Nieuws&SubUrl=nieuws.asp&DbTable=binnenland" class="page">
lees verder </a></font>

</td>
    <td width="5" valign="top" bgcolor="#FFFFFF"><font size="2" face="Verdana"><font face="Times New Roman, Times, serif"></font></font></td>
    <td width="112" valign="top" bgcolor="#FFFFFF"><strong>    </strong><img src="http://portal.telegraafnet.nl/weeranimatie.gen.gif"><br>
      <strong>Naar het strand</strong></td>
    <td width="9" background="art/right.gif" bgcolor="#FFFFFF"></td>
  </tr>
  <tr>
    <td width="9" height="9" background="art/botleft.gif"></td>
    <td height="9" colspan="4" background="art/bottom.gif" bgcolor="#FFFFFF"></td>
    <td width="9" height="9" background="art/botright.gif"></td>
  </tr>
</table>
<img src="art/clearpixel.gif" height="5"><table width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="50%" valign="top"><table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
          <td width="9" height="9" background="art/topleft.gif"></td>
          <td height="9" background="art/top.gif" bgcolor="#FFFFFF"></td>
          <td width="9" height="9" background="art/topright.gif"></td>
        </tr>
        <tr>
          <td width="9" background="art/left.gif" bgcolor="#FFFFFF"></td>
          <td height="200" valign="top" bgcolor="#FFFFFF">
  <font size="5">Nobelprijs voor moslimvrouw</font><br>
  <font color="#BF1F0B" size="1">26.10.2003 | Nieuws Buitenland</font><br>
  <font size="4"><img src="art/upload/ebadi.jpg" width="152" border="0" align="left">De Nobelprijs voor Vrede is voor het eerst toegekend aan een moslimvrouw. Deze belangrijkste ondersc...<a href="page_view.asp?normpos=0&titel=Buitenland&Sub=Nieuws&SubUrl=nieuws.asp&DbTable=buitenland" class="page">lees
  verder</a></font>
  </td>
          <td width="9" background="art/right.gif" bgcolor="#FFFFFF"></td>
        </tr>
        <tr>
          <td width="9" height="9" background="art/botleft.gif"></td>
          <td height="9" background="art/bottom.gif" bgcolor="#FFFFFF"></td>
          <td width="9" height="9" background="art/botright.gif"></td>
        </tr>
      </table>
    </td>
    <td width="5" valign="top"><img src="art/clearpixel.gif" width="5" height="1"></td>
    <td width="50%" valign="top"><table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td width="9" height="9" background="art/topleft.gif"></td>
        <td height="9" background="art/top.gif" bgcolor="#FFFFFF"></td>
        <td width="9" height="9" background="art/topright.gif"></td>
      </tr>
      <tr>
        <td width="9" background="art/left.gif" bgcolor="#FFFFFF"></td>
        <td height="200" valign="top" bgcolor="#FFFFFF">
<font size="5">Nederlanders dol op huisdieren</font><br>
<font color="#BF1F0B" size="1">26.10.2003 | Kort over Holland</font><br>
<font size="4"><img src="art/upload/huisdieren.jpg" width="152" border="0" align="left">De helft van de Nederlandse gezinnen heeft een huisdier. Een hond of kat, soms een tamme rat, slang ...<a href="page_view.asp?normpos=0&titel=Kort over Holland&Sub=Nieuws&SubUrl=nieuws.asp&DbTable=kortoverholland" class="page">lees
verder</a></font>

        </td>
        <td width="9" background="art/right.gif" bgcolor="#FFFFFF"></td>
      </tr>
      <tr>
        <td width="9" height="9" background="art/botleft.gif"></td>
        <td height="9" background="art/bottom.gif" bgcolor="#FFFFFF"></td>
        <td width="9" height="9" background="art/botright.gif"></td>
      </tr>
    </table></td>
  </tr>
</table><img src="art/clearpixel.gif" height="5"><table width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="9" height="9" background="art/topleft.gif"></td>
    <td height="9" colspan="4" background="art/top.gif" bgcolor="#FFFFFF"></td>
    <td width="9" height="9" background="art/topright.gif"></td>
  </tr>
  <tr>

    <td width="9" background="art/left.gif" bgcolor="#FFFFFF"></td>
    <td width="160" align="left" valign="top" bgcolor="#FFFFFF">&nbsp;</td>
    <td valign="top" bgcolor="#FFFFFF">&nbsp; </td>
    <td width="5" valign="top" bgcolor="#FFFFFF"><font size="2" face="Verdana"><font face="Times New Roman, Times, serif"></font></font></td>
    <td width="112" valign="top" bgcolor="#FFFFFF"><strong> </strong></td>
    <td width="9" background="art/right.gif" bgcolor="#FFFFFF"></td>
  </tr>
  <tr>
    <td width="9" height="9" background="art/botleft.gif"></td>
    <td height="9" colspan="4" background="art/bottom.gif" bgcolor="#FFFFFF"></td>
    <td width="9" height="9" background="art/botright.gif"></td>
  </tr>
</table>
</body>
<script type="text/javascript" language="javascript1.2" src="jscript/menu.js"></script>
</html>
0
Michel PlungjanIT ExpertCommented:
If I load that inot a browser I get the following links:
.../page_view.asp?titel=Wat&DbTable=wat&lan=1
.../page_view.asp?titel=Hoe&DbTable=hoe&lan=1
.../voorpagina.asp?lan=1
.../page_view.asp?normpos=0&titel=Binnenland&Sub=Nieuws&SubUrl=nieuws.asp&DbTable=binnenland&lan=1
and so on
0
Michel PlungjanIT ExpertCommented:
If I am not mistaken that is exactly what you asked for???
0
voermanAuthor Commented:
Hmm thats strange please go to www.voerman.net/babelexpress and  click through the first two pages (doesn't matter where you click). On the frontpage you see that it doesn't work.( I did send you the code for the front page)
0
Michel PlungjanIT ExpertCommented:
I get

Error: exclude is not defined
Source File: http://www.voerman.net/babelexpress/jscript/menu.js
Line: 4
0
voermanAuthor Commented:
I sent you the files by email , ok ?
0
Michel PlungjanIT ExpertCommented:
I am off to bed.
If you sent me asp files I am afraid I cannot do much with them

Please fix the error in the script or the rest of scripts may not run
0
voermanAuthor Commented:
Ok bummer, are you still getting this error ? I am not getting it..
0
David H.H.LeeCommented:
voerman ,
>>Yeah I made this test page and it works ! But still not in my "real" page.
-This mean it happend when you include the file?
0
voermanAuthor Commented:
Yeah, probably but I tried putting it in the page itself before and after the include statement but that doesn't work either.
0
voermanAuthor Commented:
Yeah, probably but I tried putting it in the page itself before and after the include statement but that doesn't work either.
0
David H.H.LeeCommented:
voerman,
It mean the solutions didn't work at all?confuse now....
0
ZontarCommented:
I suspect the menu script also has a window.onload handler which is interfering with mplungjan's -- mplugjan's code is fine, it should work without any problem.

That menu script looks fairly old (tests for NS4) and I think it could be written much better.
0
Michel PlungjanIT ExpertCommented:
true...
If you find
window.onload=something
in the code you need to to
window.onload=initpage

and add
function initPage() {
   something();
   addLang()
}

Michel
0
Michel PlungjanIT ExpertCommented:
1. add exclude=false to wherever you have the browser detection script
2. change in all menuxx.js the part
window.onload=loadFunction;
to
window.onload=initPage;
and add
function initPage() {
  loadFunction;
  addLang()
}
0
Michel PlungjanIT ExpertCommented:
sorry

function initPage() {
  loadFunction();
  addLang();
}
0
voermanAuthor Commented:
Ok I am gonna try all solution, thanks already. You here from me.
0
Michel PlungjanIT ExpertCommented:
exclude is nog STEEDS undefined
0
voermanAuthor Commented:
Cool ! I changed "window.onload" in the jscript that is valled ie5.js to

window.onload=initPage;
function initPage() {
  loadFunction();
  addLang();
}

And it works... In the HTML links, but I NOT in the menu that is generated with jscript. you can see things at

http://www.voerman.net/babelexpress/voorpagina.asp?lan=1

Any ideas ?

0
voermanAuthor Commented:
Ahh je spreekt nederlands ? Dat praat een stuk makkelijker.
0
Michel PlungjanIT ExpertCommented:
You need to add exlude
for example here:
exclude=false
function initPage()....
0
voermanAuthor Commented:
Can anybody explain why it doesn't include the lan= to the javascript links ?
0
ZontarCommented:
Maybe because they've not yet been generated when the addLang() function runs?
0
Michel PlungjanIT ExpertCommented:
javascript links as in popups?
I only add the lan to the href="xxx.asp" if no asp, not adding
If you need the others too then change to


function addLang() {
   for (i=0;i<document.links.length;i++) {
       qPos = document.links[i].href.indexOf('?');
       if (qPos !=-1) document.links[i].href+='&lan='+lan
       else document.links[i].href+='?lan='+lan
   }
}

Also I am not kidding when I ask you to ass
exclude=false somewhere in the script that runs before the menu runs
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Michel PlungjanIT ExpertCommented:
That was ADD ;)
0
ZontarCommented:
Yeah what he said. Um... the second time that is. ;-)

Seriously -- When a clientside script hits an error, it stops dead in the water -- pfffffffft!, alles ist kaputt. Fix the exclude variable already, because there's no way to tell if anything else is working correctly until you do, okay?

Ta.
0
voermanAuthor Commented:
Thx guys ! I have it working now ! I am gonna award the question to mplungjan.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.