Question about parsing a number of out a URL

1526
6
Jump to solution
12-03-2014 01:01 PM
IanPeebles
Occasional Contributor III

Good afternoon.  I have a case where I need to parse a number out of a web url and store it within a data input form.

The valid application url is this:  http://<machinename>/<applicationname>

However, I need to send a URL by clicking on the button that reads something like this:  http://<machinename>/<applicationname>/19305

Of course this is not a real application site, so as the application is loading, I need to parse out 19305 so it reads http://<machinename>/<applicationname>.  I then need to take the parsed value, then send it to an input form so it will locate within an application.

Has anyone done this?

Ian

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
KellyHutchins
Esri Frequent Contributor

Ian

If you want to be able to click a button that goes to http://machine/app/19305 but really gets loaded as http://machine/app you'll need a server side piece to handle the routing. To get that question answered you may want to post on a site like Stack Overflow.

To handle if from the client side you'd want to go with the url parameter approach so when users clicked on the button they'd go to http://yourappurl?id=19305

Then you could use javascript like Paul and I posted above to get the id value from the url.

View solution in original post

0 Kudos
6 Replies
PaulCrickard
New Contributor III

for this:

http://machinename/applicationname?n=19305

var params = {};

if (location.search) {

    var parts = location.search.substring(1).split('&');

    for (var i = 0; i < parts.length; i++) {

        var nv = parts.split('=');

        if (!nv[0]) continue;

        params[nv[0]] = nv[1] || true;

    }

}

var thenumber = Number(params.n);

thenumber now  = 19305.

KellyHutchins
Esri Frequent Contributor

Can you use url parameters instead? So your url would be something like:

http://myserver/myapp/index.html?id=19305

The JSAPI has a helper method that makes it easy to extract the parameter from the url doing something like this:

var urlObject = urlUtils.urlToObject(url);
if(urlObject.query && urlObject.query.id){
     var myId = urlObject.query.id;
}
 

And here's a link to a sample that shows this in action:

https://developers.arcgis.com/javascript/jssamples/exp_history.html

IanPeebles
Occasional Contributor III

So if I click a button that goes to http://machine/app/19305 the application will actually be loaded as http://machine/app?  This is what I am wanting.  The http://machine/app/19305 URL is actually not a real URL, but I want to capture the data while the app is loading.  I hope this makes sense.

Attached is a copy of my code.

Ian

0 Kudos
KellyHutchins
Esri Frequent Contributor

Ian

If you want to be able to click a button that goes to http://machine/app/19305 but really gets loaded as http://machine/app you'll need a server side piece to handle the routing. To get that question answered you may want to post on a site like Stack Overflow.

To handle if from the client side you'd want to go with the url parameter approach so when users clicked on the button they'd go to http://yourappurl?id=19305

Then you could use javascript like Paul and I posted above to get the id value from the url.

0 Kudos
IanPeebles
Occasional Contributor III

Thank you for the responses!  I actually looked at the sample from ESRI and that works extremely well.  I referred to the sample and now have a web application set up that does exactly what I need it to.  Starting to like AMD more and more

KellyHutchins
Esri Frequent Contributor

Great news Ian. Glad you have your app up and running.

0 Kudos