How do I get access to Browser.QueryParam Expression in Javascript? Or similar?

0 favourites
  • 7 posts
  • Hello! I'm trying to get access to the Browser Object in Javascript to use it's QueryParam expression. I've read in the documentation that using expressions in JS isn't possible.

    Is there something similar to it that I could use? I've tried putting in my function Browser.QueryParam("myparam"); but it gave me "Browser is not defined" error in the console.

    Does anyone know what could I do? I'm trying to get an auth token. Code below:

    function GettingData()
    {
    	const url = "https://testurl.com/api/testing-page"; // API URL
    	const token = Browser.QueryParam("mytokentest"); // API Token - Does not Work. Console gives Browser Not Defined
    	//const token = runtime.objects.Navegador; - Navegador = Browser Object in Portuguese. Can't access QueryParam from here.
    	const method = "GET"; // Request method
    
    	fetch(url, {
    		method,
    		headers: {
    			"Authorization": `Bearer ${token}` // Auth header
    		}
    	}).then(res => res.json().then(console.log)).catch(console.error); // Do better handling here
    }
  • You don't need to do this via the Browser object, just google how to get query parameters with Javascript, there are several methods. For example:

    flaviocopes.com/urlsearchparams

  • You don't need to do this via the Browser object, just google how to get query parameters with Javascript, there are several methods. For example:

    https://flaviocopes.com/urlsearchparams/

    Hey, thanks for your insight!

    I'm new to JS, so I don't even know exactly what to search for sometimes. I'll make sure to test some of these.

    Do you happen to know if those methods usually works even if the param is "hidden" from the address bar?

  • Sorry, I'm not sure what a 'hidden' parameter is..

    The easiest way to know for sure is to test.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Sorry, I'm not sure what a 'hidden' parameter is..

    The easiest way to know for sure is to test.

    I don't know if this is the right nomenclature for it, but there's sometimes when a parameter isn't showed in the URL at the address bar, like for example https://examp.com/?myparam="abcd1234". In my case the link is like "https://examp.com/mygame" and the parameter is actually inside the page in an <iframe src="https://examp.com/?myparam="abcd1234">.

    I don't get too much of web as of now to understand if what I did was necessary or not, but the only way I got it to work is as it follows (using the URLSearchParams api). To anyone who stumbles upon something like this in the future there goes what I did bellow. Also, thanksdop2000, this API was in the link you recommended. :)

    	globalThis.paramGotten = "Initialize";
    
    function gettingParameter()
    {
    	const param = new URLSearchParams(window.location.search);
    	//if there is a param called myparam...
    	if(param.has('myparam'))
    	{
    		console.log("param exists");
    		//passing the param that was found to the Global Variable "paramGotten"
    		//this way it's possible to access it in the EventSheet
    		paramGotten = param.get('myparam');
    		console.log(paramGotten);
    	}
    }
    

    I tried using localVars and globalVars to get the param from the JS code, but wasn't able to do it in anyway. The only path I found was with the Text Object. But hey, it's working.

  • Glad it works! You should be able to get the result into a global variable like this:

    runtime.globalVars.VarName = paramGotten;
    
  • Glad it works! You should be able to get the result into a global variable like this:

    > runtime.globalVars.VarName = paramGotten;
    

    Hey, it worked! Don't remember what I was doing wrong, but your line did the trick. Thank you for helping out!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)