Solved

Read URL Parameter using Javascript then pass on Parameter to another URL

Posted on 2004-08-18
3
3,258 Views
Last Modified: 2011-09-20
Hi

My company's server currently cannot use dynamic scripting languages such as PHP, ASP or JSP, etc. (for reasons I won't go into here!). Therefore my only option is javascript.

What I need to do:

Read the URL Parameter ID - example - http://www.myexample.com?ID=1)
Pass into another URL - example - http://www.myexample2.com
and redirect the page to new URL - example - http://www.myexample2.com?ID=1

Is this possible?

Thanks for your time
N
0
Comment
Question by:nelliott
  • 2
3 Comments
 
LVL 15

Accepted Solution

by:
Colosseo earned 125 total points
Comment Utility
Hi N

This code will check for the existence of a ? in the current url.
If found it will add it to the new url and redirect there

The only problem is that on my server using your example url i get output of

http://www.myexample2.com/?id=1

I think this is because there is no page sepcified. I dont know if that is a problem but if you know the page that you are redirecting to on the site you can use

new_URL = "http://www.myexample2.com/page1.htm" + current_URL.substring(current_URL.indexOf('?'));

<script language="JavaScript">

  // Get the current url
  current_URL = document.location.href;
  // If a ? is in the url
  if (current_URL.indexOf('?') != -1) {
   // Concatenate the new url with everthing after the ?
    new_URL = "http://www.myexample2.com" + current_URL.substring(current_URL.indexOf('?'));
    // redirect tothe new url
    window.location = new_URL;
  }
 
</script>

Cheers

Scott
0
 

Author Comment

by:nelliott
Comment Utility
Pure genius.

Works like a charm.

Thanks for your time
N
0
 
LVL 15

Expert Comment

by:Colosseo
Comment Utility
Your welcome, glad its working

Thanks for the Grade

Scott
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
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…

772 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