How to pass literal parameter to included js?
Majidi Karachi
How to pass literal parameter {$currchoice.id}
to js? I have the following currency picker and I need it to pass the selected value to javascript as the param (currency=2)
is(currency={$currchoice.id})
<!-- Currency -->
{if !$loggedin && count($currencies) > 1}
<div class="pull-right nav">
<a href="#" class="quick-nav" data-toggle="popover" id="currencyChooser"><i class="fa fa-usd"></i> {$LANG.choosecurrency} <span class="caret"></span></a>
<div id="currencyChooserContent" class="hidden">
{foreach from=$currencies item=currchoice}
<img src="{$BASE_PATH_IMG}/flags/{$currchoice.code|substr:0:2|strtolower}.png"> <a href="{$currentpagelinkback}currency={$currchoice.id}">{if $currency.id eq $currchoice.id}<u>{/if}{$currchoice.code}</u></a>
{/foreach}
</div>
</div>
{/if}
<script language="javascript" src="feeds/productsinfo.php?pid=1&get=price&billingcycle=monthly¤cy=2"></script>
top master
You seem to have a popover
-menu where the user can select a currency and then ask how to add another script based on the user's choice.
Well, if you prefer to refresh the page, just pass the URL parameter from the controller to the view, like:
public function getMyView($request) {
// TODO: replace 0 with your default currency-id.
return view('path.to.view')
->with('selectedCurrency', $request->input('currency') ?? 0)
}
and use it in the view like:
<script language="javascript" src="feeds/productsinfo.php?pid=1&get=price&billingcycle=monthly¤cy={{$selectedCurrency}}"></script>
However, if you don't want to refresh every time the user clicks, then:
<script ...
Remove the thing completely from your view .- Add
JQuery
code (or something similar) to find the selected value (e.g. currency ID). - Finally,
script
useJavaScript
.
Note that the second approach requires your entire JS (and content that depends on it) to support lazy loading (without causing errors).