cancel
Showing results for 
Search instead for 
Did you mean: 

SAPUI5 get URL parameters

former_member182500
Contributor
0 Kudos

Hi,

I have a URL.  Example:

datalink.html?param1=1&param2=2&param3=3

The URL points to a .HTML with a SAPUI5 view placed in the body content.  In the .HMTL I also include some .js files.  Within a .js file I am trying to get the parameters of the URL, using:

$.request.parameters.get("param1"')

The view displays OK however viewing the browser console I see a runtime error indicating "request" is undefined.  All I'm trying to do is retrieve the URL parameters, I have searched SCN, google and a quick scan of the SAPUI5 developer guide without joy.  I really must be missing a trick.

Note I am not trying to get URL parameters from within an XSJS service (where above jQuery works fine).  It is from within pure SAPUI5 (or rather .JS files included in the HTML, or as an alternative within the view controller.js).

Many thanks.

Jon-Paul.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member

Try this code:

var complete_url = window.location.href;

  var pieces = complete_url.split("?");

  var params = pieces[1].split("&");

  $.each( params, function( key, value ) {

       var param_value = value.split("=");

       console.log( key + ": " + value + " | " + param_value[1] );

  });

References:

https://api.jquery.com/jQuery.each/

Get url parameter jquery - Stack Overflow

former_member182500
Contributor
0 Kudos

Thanks, I'll give that a try first thing tomorrow.

Just to add, the .html detailed above is embedded within an iframe of a html control in a parent view, so hoping window.location.href in child.html refers to this child rather than parent.html.

I would have thought there would have been a sap.ui.core or sap.ui.common function for this "bread and butter" requirement, hmmm.

jmoors
Active Contributor

The SAPUI5 library does provide a method.


var sValue = jQuery.sap.getUriParameters().get("myUriParam");

Regards,

Jason

Answers (5)

Answers (5)

tobiassorn85
Explorer

The modern way would be:

sap.ui.require(["sap/base/util/UriParameters"], function(UriParameters) {

var sParam = UriParameters.fromQuery(window.location.search).get("param1");

});

@see: https://sapui5.hana.ondemand.com/#/api/module:sap/base/util/UriParameters

Koen_VL
Participant
0 Kudos

Hi tobiassorn85

I tried this, but get:

entry.controller.js:17 Uncaught (in promise) TypeError: UriParameters.fromQuery is not a function

as an error.

I use this code in the onInit function

Any idea what I'm doing wrong?

Koen

cyb3rko
Discoverer
0 Kudos

As tobiassorn85's solution is deprecated again, here's the current solution via the web standard class 'URLSearchParams' (not UI5 specific):

let searchParams = new URLSearchParams(window.location.search);
let param1 = searchParams.get("param1");

See deprecation notice at https://sapui5.hana.ondemand.com/#/api/module:sap/base/util/UriParameters.

0 Kudos

This message was moderated.

Former Member
0 Kudos

Hi Jon,

I have created the Application project and deploy in the SAP HANA cloud Platform and its working fine.

I have some Input Fields that i want to fill using URL parameters.

https://myloginpXXXXXXXXtrial.hanatrial.ondemand.com/myui2/index.html?&Field1=Valu1&Field2=Valu2

I have created the Java Application in that 3 different files

1) Viewname.view.js

2) Viewname.controller.js

3) Index.html

I have declare thies 2 fields under the Viename.view.js

How to Fill these 2 fields from URL parameters values.

Please suggest me where i can write code and what ?

Many Thanks,

Mithun

former_member182500
Contributor
0 Kudos

Both methods tested and work, including the parameters passed with a HTML href in an iframe child of a parent page.  Many thanks gentlemen.