I'm trying to create an if statement which shows a line of JavaScript only if the current page URL matches a given string. Here's some pseudo code:
if URL matches ("example.com/sitename/") then
response.Write("<script='file.js'></script>")
end if
The problem is how do I check whether the URL of the current page matches the string?
Many thanks!
Thanks for your responses. I'm trying to make it a little more complicated with an array of possible URL matches. I've created the following asp, but it doesn't work properly. The script is being written on a page where the URL doesn't match. Is there anything that looks wrong? I can't figure it out!
<%
Dim pageURL
pageURL = Request.ServerVariables("SERVER_NAME") & Request.ServerVariables("URL")
URLArray = Array("example.net/folder1/","example.net/folder2/")
For Each URLsnippet In URLArray
if instr(pageURL, URLsnippet) then
response.Write("<!--BEGIN EXCLUDE--><script type='text/javascript' src='script.js'></script><!--END EXCLUDE-->")
else
end if
Next
%>
It should match URLs matching example.net/folder1 and example.net/folder2, but it's also matching example.net folder3!
try
if Request.ServerVariables("URL") = "/myurl.com/script.asp" then
' do stuff
end if
If you just want the url try this
var url = "<%=Request.ServerVariables("SERVER_NAME") & Request.ServerVariables("URL")";
if(url == "site.com/script.asp")
{
//Do stuff
}
If you also need the querystring try
var url = "<%=Request.ServerVariables("SERVER_NAME") & Request.ServerVariables("URL") & "?" & Request.Querystring%>";
if(url == "site.com/script.asp?var=123")
{
//Do stuff
}
Related
I'm trying to add the value from my query string to a response redirect. I've added my code below, any help would be great.
If Boolean.Parse(ConfigurationManager.AppSettings.Item("Development")) Then
If Request.QueryString("ABC") = "Y" Then
Session("Website") = "abc"
End If
If Not Request.QueryString("LOCATION") = "" Then
Response.Redirect "mysite.co.uk/location/" & Request.QueryString("LOCATION")
End If
End If
Response.Redirect("Yourpage.aspx?UserId="+location);
What happens when you use:
Response.Redirect("http://mysite.co.uk/location/" & Request.QueryString("LOCATION"))
I am working on classic asp application.
I have use URL rewrite on some pages.
How can i get current url of the page in classic asp?
Example:
http://www.site.com/page.asp ---> url rewrite in IIS ---> http://www.site.com/home/page
so here i want current url of the page which is http://www.site.com/home/page
Please help me.
Thanks.
There's no fancy one function that does it all.
First you need to get the protocol (if it is not always http):
Dim protocol
Dim domainName
Dim fileName
Dim queryString
Dim url
protocol = "http"
If lcase(request.ServerVariables("HTTPS"))<> "off" Then
protocol = "https"
End If
Now the rest with optional query string:
domainName= Request.ServerVariables("SERVER_NAME")
fileName= Request.ServerVariables("SCRIPT_NAME")
queryString= Request.ServerVariables("QUERY_STRING")
url = protocol & "://" & domainName & fileName
If Len(queryString)<>0 Then
url = url & "?" & queryString
End If
Hope it works for you.
You can try to output all ServerVariables like so:
for each key in Request.Servervariables
Response.Write key & " = " & Request.Servervariables(key) & "<br>"
next
Maybe the URL you seek is already there. We use the Rewrite module and there is a ServerVariable called HTTP_X_ORIGINAL_URL that contains the rewritten URL path, e.g. "/home/page" in your example.
Protocol (HTTPS=ON/OFF) and Server (SERVER_NAME) can also be found in the ServerVariables.
If you use URL Rewrite, the url data can only be retrieved in this way:
Request.ServerVariables("HTTP_X_ORIGINAL_URL")
Example
Dim domainName, urlParam
domainName = Request.ServerVariables("SERVER_NAME")
urlParam = Request.ServerVariables("HTTP_X_ORIGINAL_URL")
response.write(domainName & urlParam)
I am trying to render a hyperlink in an email template which will be sent to the user and if the user clicks on that link it will direct to a unique url. I have given the coding below,
email.AddMailmerge("RequestUrl", "Feedback Requests")
My problem is the link doesnt resolve correctly and take me to the right url. What am I doing wrong?
It resolves as:
C:\Users\Test\Desktop\localhost/Requests/Requests.aspx?Company_ID=KirprZ17bg5u5Qf1
Make sure you Appsetting contains http:// so instead of just
localhost
it needs to be
http://localhost
Be sure to format your href with quotes. Instead of relying on your app.config's value, which is return the C:\ value, use the .NET methods to get the current domain/URL.
Dim target As String = _
String.Format("<a href='{0}/Requests/Requests.aspx?Company_ID={1}'>Feedback Requests</a>",_
Request.Url.GetLeftPart(UriPartial.Authority),_
objCompany.IDHashed)
Dim strRequestLink As String = System.Configuration.ConfigurationManager.AppSettings("DomainName") & "/Requests/Requests.aspx?Company_ID=" & Me.ID & "&key=" & Me.IDHashed
Dim strRequestUrl As String = "http://" & strRequestLink & ""
I used the above formatting.
I am using
Dim url As String = Server.MapPath("MyFiles") & "\" &
System.IO.Path.GetFileName(hpf.FileName)
to get the url of the uploaded file.
The result i get is
D:\2008VS\LTC INDIA\LTCIndia v 1.52\AdminPanel\MyFiles\1.jpg
i want it to be like this http://www.abcd.com/AdminPanel/MyFiles/1.jpg
Kindly let me know if posting the whole code would be of any help.
You could use the ResolveUrl method:
Dim url As String = ResolveUrl("~/MyFiles/" & System.IO.Path.GetFileName(hpf.FileName))
i want to set a link in VB.net dynamically to a file.
My url looks like that:
http://server/folder/folder2/file.aspx?get=param
I tried to use Request.URL but i have not found any solution to get only
http://server/folder/folder2/
without the query string and without the filename.
Please help.
Dim url = Request.Url;
Dim result = String.Format(
"{0}{1}",
url.GetLeftPart(UriPartial.Authority),
String.Join(string.Empty, url.Segments.Take(url.Segments.Length - 1))
)
You can easily get a relative file path using the Request instance, then work with that, using Path class ought to help:
Dim relativePath = Request.AppRelativeCurrentExecutionFilePath
Dim relativeDirectoryPath = System.IO.Path.GetDirectoryName(relativePath)
It's worth noting that GetDirectoryName might transform your slashes, so you could expand the path:
Dim mappedPath = HttpContext.Current.Server.MapPath(newpath)
So, to remove redundancy, we could shorten this:
Dim path = _
Server.MapPath( _
Path.GetDirectoryName( _
Request.AppRelativeCurrentExecutionFilePath)))
But you'll need to check for possible exceptions.
You can use Uri.Host to get the computer name and then Uri.Segments (an array) to get everything up to the filename, for example:
var fileName = Uri.Host && Uri.Segments(0) && Uri.Segments(1)
This will give you: server/folder/folder2
If you have a variable number of segments, you can iterate over them and ignore the last one.
I hope that might help :)