? Or & Createurl

I need to call a route with parameters from java script and I would like it to work independent of the URL format (enable URLs in path-format)

My problem is, in path format I need to append ‘?’ and in the other format ‘&’.

My current workaround is to use a dummy parameter as




$urlGetProfile = $this->createUrl('controller/getProfile', array('dummy' => 'param'));



so that javascript can do




var url = '$urlGetProfile' + '&name=' + studentName;



Is there a better solution?

You can use JS string functions to see what your url has ended with.

And here’s the funky way:


$urlGetProfile = $this->createUrl('controller/getProfile', array('name' => '__name__')); //placeholder for params




function createUrl(url, data) {

    return url.replace(/__([\w-]+)__/g, function(match, attr) {

        return data[attr];

    });

};


var url = createUrl('<?= $urlGetProfile ?>', {name: studentName, ...});



:blink: