Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to get a variable from a URL

Posted on 2011-09-29
13
Medium Priority
?
227 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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 2000 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 Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
Suggested Courses

971 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