Start html5 app with parameter

Hello,

I wanted to make a html5 app that would present laser tag game score to customers.
Each game has its GameID as inique identifier.

Idea is that costumer scans QR with url (any QR scaner app) and is sent to Solar 2D built app that would show data for this particular game.

Is there a way to get GameID to html5 app build with Solar? My initial idea is that it would be part of url, as in php “GET” method.

Im happy for any suggestions or ideas on this topic.
Jiri

You can accomplish this by creating a JS plugin and running it when your app starts.

1 Like

Maybe this could help - New Plugin: Run JavaScript without Leaving Lua

3 Likes

That plugin isn’t really necessary and you’ll most likely get better performance by writing a simple JS plugin yourself.

There are clear samples at https://docs.coronalabs.com/guide/html5/plugins/index.html or you could copy the basics from Solar2D Playground’s source code: https://github.com/XeduR/solar2dplayground.com.

You really don’t need anything else than a simple JS plugin that runs a function like this: https://gomakethings.com/getting-all-query-string-values-from-a-url-with-vanilla-js/ and returns the values back.

Thank you both for tips and links very much!
I used that last link posted by @XeduR to get parameters from url.

Here is how I did it in case someone would use it:

url

someweb.com?gameid=somegameid

main.lua

local jsp = require ( "jsurlparameter" )

local params = jsp.get()
local GameID = params.gameid

display.newText (
	{
  text = GameID or "N/A",
  x = display.contentCenterX,
  y = display.contentCenterY,
  font = native.systemFont,
  fontSize = 18
}
)

in jsurlparameter.lua (wrapper for simulator).

if system.getInfo("platform") == 'html5' then
   return require "jsurlparameter_js"
else
   local lib = {}
   setmetatable( lib, {__index = function( t, k )
       return function() 
         print( "WARNING: Placeholder is called for " .. k )
       end
    end} )
lib.get = function ()
		return ( { gameid = "7777-d" } ) --for testing in simulator
	end
  return lib
end

finally in jsurlparameter_js.js

jsurlparameter_js = {
	get : function () {
    	return getParams(window.location.href);
	},
}

/**
 * Get the URL parameters
 * source: https://css-tricks.com/snippets/javascript/get-url-variables/
 * @param  {String} url The URL
 * @return {Object}     The URL parameters
 */

var getParams = function (url) {
	var params = {};
    var parser = document.createElement('a');
    parser.href = url;
	var query = parser.search.substring(1);
	var vars = query.split('&');
	for (var i = 0; i < vars.length; i++) {
    	var pair = vars[i].split('=');
		params[pair[0]] = decodeURIComponent(pair[1]);
    }
    return params;
};
7 Likes

That info you put right here in one place was SO helpful for what I’m working on right now. Thank you!

Jay