...

View Full Version : String Functions



arpan_de
05-13-2007, 09:04 AM
I want to find out what is the file name of the page that a user is currently viewing (for e.g. the file name of the page where I am composing this query currently is newthread.php). If the file name is Edit.asp, then do something else......this is the code (this is the onLoad event function)
function getFile(){
var strURL=window.location

//since the file name will be at the end of the URL, I am using
//a negative number as the start parameter in the substr function

if(strURL.substr(-9,strURL.length)=="Edit.asp"){
//do something
}
else{
//do something else
}
}
But the above doesn't work. In fact, some of the JavaScript string functions I tried (like match, length etc.), none of them seem to work since the string has been derived using window.location. Why? Doesn't JavaScript consider the URL of a page as a string object?

Can someone please tell how do I resolve this?

_Aerospace_Eng_
05-13-2007, 09:44 AM
Does asp not have a method for getting the current uri? In php you could use
<?php echo $_SERVER['REQUEST_URI']; ?>
which would give you the current url that you are on. You should be using

var strURL=window.location.href
That should get things working though I think you should be doing this server side anyways.

rwedge
05-13-2007, 10:57 AM
Here is an expample:
function getFile(){
var strURL = self.location.href.split('/');
if(strURL[strURL.length -1] == "Edit.asp"){
//do something
}
else{
//do something else
}
}

arpan_de
05-13-2007, 12:30 PM
Thanks rwedge, your code works exactly as what I wanted but what's wrong with the code I cited in my post? Why doesn't it work?

Thanks to Aerospace too for his suggestion but sorry to say, Aerospace, I am not fully convinced with your statement that I should be doing this server-side rather than client-side. I feel when & where one needs to use client-side programming & server-side programming entirely depends upon the application he is working on, isn't it?

Anyway thanks once again to both of you,

Regards,

Arpan

_Aerospace_Eng_
05-13-2007, 06:53 PM
Thanks rwedge, your code works exactly as what I wanted but what's wrong with the code I cited in my post? Why doesn't it work?

Thanks to Aerospace too for his suggestion but sorry to say, Aerospace, I am not fully convinced with your statement that I should be doing this server-side rather than client-side. I feel when & where one needs to use client-side programming & server-side programming entirely depends upon the application he is working on, isn't it?

Anyway thanks once again to both of you,

Regards,

Arpan

Thats true but you didn't quite tell us what you were working on so how can we know? :rolleyes:

If you weren't too busy trying to say I was wrong you would have noticed why your original script wasn't working. Reread my post again.

glenngv
05-14-2007, 08:31 PM
function getFile(){
var strURL=window.location.href;
//or
//var strURL=window.location.pathname;
//var strURL="<%=Request.ServerVariables("URL")%>";

if(strURL.toLowerCase().indexOf("edit.asp") != -1){
//do something
}
else{
//do something else
}
}

felgall
05-14-2007, 09:50 PM
why are you referencing location.href when you are looking for part of location.path



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum