Solved

How to get a variable from a URL

Posted on 2011-09-29
13
215 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

747 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

11 Experts available now in Live!

Get 1:1 Help Now