Solved

How to get a variable from a URL

Posted on 2011-09-29
13
216 Views
Last Modified: 2013-11-05

Can anyone give me the javascript code that will give me the value of the path variable before the last "/" and after the 2nd last "/" . In this case it is ITCSLIDE. I have a bunch of these url's and I need to get that value from each.

https://wwwdev.acponline.org/ebizbtest/ProductsandServices/OnlineStore/ProductListing/tabid/56/Class/ITCSLIDE/Default.aspx
0
Comment
Question by:ptrick_whelan
  • 6
  • 5
  • 2
13 Comments
 
LVL 13

Expert Comment

by:haloexpertsexchange
ID: 36817402
your link is not working.
0
 

Author Comment

by:ptrick_whelan
ID: 36817457

That's an example of the link not the actual link. Actually if someone could just tell me how to do this in javasctrip for the URL

If this URL "https://wwwdev.acponline.org/ebizbtest/ProductsandServices/OnlineStore/ProductListing/tabid/56/Class/ITCSLIDE/Default.aspx
contains ITSCLIDE
then do someting
0
 
LVL 2

Expert Comment

by:EndersDev
ID: 36817499
function extractFromPath( path )
{
var re=/\/([^\.\/]+)\/([^\.\/]+\.[^\.\/]+)$/.exec( path );

return re ? re[1].replace(/\.[^\.]+/,'') : "" ;
}

alert(
extractFromPath('https://wwwdev.acponline.org/ebizbtest/ProductsandServices/OnlineStore/ProductListing/tabid/56/Class/ITCSLIDE/Default.aspx'))
0
 
LVL 13

Expert Comment

by:haloexpertsexchange
ID: 36817528
This works provided that you are always going to be looking for the folder level right above the end of the url.
var url = "https://wwwdev.acponline.org/ebizbtest/ProductsandServices/OnlineStore/ProductListing/tabid/56/Class/ITCSLIDE/Default.aspx";
var splits = url.split("/");
document.write(splits[splits.length-2]);

Open in new window

0
 

Author Comment

by:ptrick_whelan
ID: 36817735
The extractFromPath function works great but I do not know how to use the function in an IF situation. I need to display a certain heading and paragraph on as aspx page depending on what is returned.
0
 

Author Comment

by:ptrick_whelan
ID: 36817799

example

If extractFromPath is ITCSLIDE
"<h4>These are the slide</h4>"

If extractFromPath is SUBS
"<h4>These are the subscriptions</H4>"

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:EndersDev
ID: 36817841
You only asked for an extraction method, but here is the if/else:

if( extractFromPath( path ) == 'value1')
{
    // do something
}
else if( extractFromPath( path ) == 'value2')
{
  // do something else
}
0
 
LVL 2

Expert Comment

by:EndersDev
ID: 36817864
If you are trying to make changes to the DOM you need to get the element and then use innerHtml to set the value. But you didn't specifically ask for that so I'm guessing you have some code to help you with that portion.
0
 

Author Comment

by:ptrick_whelan
ID: 36817964

sorry but this is not doing anything

<script type="text/javascript" language="javascript">
function extractFromPath( path )
{
var re=/\/([^\.\/]+)\/([^\.\/]+\.[^\.\/]+)$/.exec( path );

return re ? re[1].replace(/\.[^\.]+/,'') : "" ;
}

if( extractFromPath( path ) == 'ITCSLIDE')
{
    document.write('These are the slides');
}


</script>
0
 
LVL 2

Expert Comment

by:EndersDev
ID: 36818232
Its not doing anything because you need to place that in a function and have something trigger it. I don't know enough about what you are trying to do to do that for you.
0
 
LVL 2

Accepted Solution

by:
EndersDev earned 500 total points
ID: 36818234
I answered your initial question though...
0
 

Author Comment

by:ptrick_whelan
ID: 36891495
if i have an html page how do I use the function to display text based on the value

<html>
<body>

What code do i put here to set a condition to use text based on the value in the URL

</boby>
</html>
0
 

Author Comment

by:ptrick_whelan
ID: 36892051

I see now what you mean. I figured it out.

Thanks again
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jQuery scrollTop 2 40
jQuery AJAX Redirects Without Response/Error 10 42
Apply tab index in forms 6 33
using php variable inside javascript 5 11
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now