Solved

How to get a variable from a URL

Posted on 2011-09-29
13
223 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

710 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