Magento - How to pass value from template to js file?

98 views Asked by At

I developed an extension where you can define a connection id for newsletter 2 go.

I need to pass this id to the script skin\frontend\base\default\fekete\Newsletter2Go\js\utils.js

Template:

My first approach was to pass it to the script by adding a get parameter to it.

{... Hint: I removed the rest code which is not relevant for this question ... }
(window,document,"script","skin/frontend/base/default/fekete/Newsletter2Go/js/utils.js?id=<?php echo $id ?>","n2g");

I checked if this worked like this:

alert(findGetParameter("id"));

function findGetParameter(parameterName) {
    var result = null,
        tmp = [];
    var items = location.search.substr(1).split("&");
    for (var index = 0; index < items.length; index++) {
        tmp = items[index].split("=");
        if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]);
    }
    return result;
}

But it alerts null.

How can I pass the id to the script, without having to move the whole script to the template?

1

There are 1 answers

0
Black On

I solved it by creating a new extension. I developed a controller and then just made a XHR Request to my route, which just returns the needed value.