Invision Community 4: SEO, prepare for v5 and dormant account notifications Matt November 11, 2024Nov 11
Posted January 30, 20169 yr I know this sounds weird, but I want to be able to use the parameters passed to a template in JS, how do I go about doing this?
January 30, 20169 yr I would suggest either add the parameters in a data- attribute on one of the elements in the template, or, if absolutely necessary (it probably isn't), add a <script> tag and define the parameters as variables in there.
January 31, 20169 yr Author yeah I tried accessing through data attributes but i cannot for the life of me get that to work. Always comes back undefined.
January 31, 20169 yr Author $('div.example').data('twurl'); I'm trying to use this code to retrieve the data from: <div id='example' data-twurl='mydata'> </div> I'm trying to do this with a widget, but it's not working, any ideas?
January 31, 20169 yr 8 hours ago, ReZnoR said: $('div.example').data('twurl'); I'm trying to use this code to retrieve the data from: <div id='example' data-twurl='mydata'> </div> I'm trying to do this with a widget, but it's not working, any ideas? Your selector is wrong - it should be $('#example').data('twurl');
January 31, 20169 yr Author 15 hours ago, Colonel_mortis said: Your selector is wrong - it should be $('#example').data('twurl'); Tried that too, still didn't work.
January 31, 20169 yr 22 hours ago, ReZnoR said: I know this sounds weird, but I want to be able to use the parameters passed to a template in JS, how do I go about doing this? Set(specifically modified to support use in a template, remove the {{}} if working in a PHP file): {{\IPS\Output::i()->jsVars['my_data_key'] = $data;}} Read in JS: var myData = ips.getSetting('my_data_key'); Using the data attributes properly tends to be a bit more complex, as you end up diving into making controllers and/or models in the js framework.
January 31, 20169 yr Author 1 hour ago, Marcher Technologies said: Set(specifically modified to support use in a template, remove the {{}} if working in a PHP file): {{\IPS\Output::i()->jsVars['my_data_key'] = $data;}} Read in JS: var myData = ips.getSetting('my_data_key'); Using the data attributes properly tends to be a bit more complex, as you end up diving into making controllers and/or models in the js framework. I don't know what I'm doing wrong, but I am doing exactly as you did here and I still cannot read the variable in the JS says undefined. The template variable only contains a URL so I don't know how else to get it there.
February 1, 20169 yr Author I checked the jsVars array, and it only contains a couple of items, including my custom one. But when I print out the object in JS to check it, it contains the info of the main jsVars object with baseURL and all of those but not my custom one. How does my js file access only stuff for that plugin? Like my jslang file, my javascript file can't access it.
Archived
This topic is now archived and is closed to further replies.