I need to insert content of variable with Smarty syntax into javascript, like below. The script is checking if page is opened from validation link in email sent to customer.
{literal}
<script>
if ((document.URL).indexOf("validation") > -1) {
$('.loginForm').append(
"<p class='middleWarningTextP loginMessage'>{/literal}{$VALIDATION_NOTICE->getMessage()}{literal}</p>");
}
</script>
{/literal}
Problem is, that this works only if javascript condition is true, otherwise page is loaded wrong: between <header> and </header> tag is nothing! and therefore css style is not loaded. I don't understand it, is there a way to repair it?
You should remove the {literal} tags. Smarty does not execute statements within these tags More info. But then, if use curly braces inside your javascript, you need to write your statements on multiple lines:
var test = {
value: 'test'
}
If you're using Smarty 3
<script>
if ((document.URL).indexOf("validation") > -1) {
$('.loginForm').append(
"<p class='middleWarningTextP loginMessage'>{$VALIDATION_NOTICE->getMessage()}{literal}</p>");
}
</script>
That is, simply remove the {literal} tags
If you're using Smarty 2 instead, i think it's something like this
<script>
if ((document.URL).indexOf("validation") > -1) {ldelim}
$('.loginForm').append(
"<p class='middleWarningTextP loginMessage'>{$VALIDATION_NOTICE->getMessage()}</p>");
{rdelim}
</script>
Problem was not with {literals} tags or syntax. Problem was that $VALIDATION_NOTICE->getMessage() variable was not set in some cases. Therefore I added this condition:
{if !is_array($VALIDATION_NOTICE) and $VALIDATION_NOTICE}{$VALIDATION_NOTICE->getMessage()}{/if}
and now it works right.
Related
So I have a HTML file with an embedded script. A Java application sends a value to this HTML file. Now I wonder how to pass this value from the HTML down to the script. Is this even possible?
Here is the simplified HTML file with my approach:
<html>
<body>
<div id="test">
[VALUE_FROM_BACKEND] // prints "let valueFromBackend = 1234"
</div>
<script>
console.log(document.getElementById('test').value);
// should return: let valueFromBackend = 1234;
// actually returns: undefined
</script>
</body>
</html>
Unfortunately, I can't pass the value from the Java application directly to the script. I got the above approach from here, but this doesn't work.
Other solutions only focus on getting values from remote HTML pages, declaring the HTML files's source in the script tag. But since it is an embedded script here, this also seems not to work.
Does anyone know how to deal with the situation? Help will be much appreciated.
Only HTML input elements have a value in javascript. A div cannot have a value, which is why your code returns undefined.
To access the text inside a regular HTML element, such as a div, use element.innerText instead.
Here is a working code snippet you can try out:
console.log(document.getElementById('test').innerText);
<div id="test">
let valueFromBackend = 1234
</div>
As you want to get value of a div element, so the syntax is:
document.getElementById('test').innerHTML
Remember that getElementById().value works for input and use getElementById().innerHTML for elements like div
I'm creating an email template that sends the user some JavaScript code that he needs to place on his website.
I've tried wrapping it in a code tag but it still treats the script tag as an actual tag instead of a string.
<p>
<code>
<script>
!function(e,t,i){var n,d=e.getElementsByTagName("head")
eElement("script")).id=i,n.setAttribute("key","kKQ1Yxgxm")
</script>
</code>
</p>
Expected result is it should treat whatever is in the code tag as a simple string. Including the tag. But it's being treated as a script tag and not as a plain string.
Escaping the < > should work.
<script>
!function(e,t,i){var n,d=e.getElementsByTagName("head")
eElement("script")).id=i,n.setAttribute("key","kKQ1Yxgxm")
</script>
<script >
for(i=0;i<=9;i++)
{
</script>
<input type="button" value="1"/>
<script type="text/javascript">
}
</script>
This puts button only 1 time if I use document.write it prints 10 times why ?
JavaScript does not behave in the same way PHP does. Whatever you place between the <script> tags is a script in and of itself.
So you have two separate scripts:
This:
for(i=0;i<=9;i++)
{
and this one:
}
Imagine what would happen if you placed these two scripts into two separate files? That's right, both would fail because of syntax errors. If you take a look at the console you'll see what errors I'm talking about.
If you want to print 10 buttons do something like this:
<div id="mainDiv">
</div>
<script>
var mainDiv = document.getElementById('mainDiv');
for(var i=0; i<10; i++){
mainDiv.innerHtml += "<input type='button' value='1'>";
}
</script>
These script tags are separate scripts. You can use functions or variables defined in the first one, but you cannot have a for loop spanning both.
The html in between is not in the script either. It looks like your trying to use script tags as if they're <?php?> ones.
Alternatives to document.write, is setting the contents of an element's innerHTML or adding an element to the DOM:
for(i=1;i<10;i++){
var btn = document.createElement("button");
document.body.appendChild(btn);
}
HTML is not a template engine as PHP so you can't separate a code block into multiple script tags. Each code block must be placed entirely in a <script></script> tag pair.
If your purpose is generate 10 buttons with Javascript, please go with document.write option
you are looking for some sort of server side templating. remember javascript runs on the client.
I have two questions:
1)- What example is better- when i use onclick in html or when i find the element by in the script?
2)- Is it correct to pass $id value by \<\?=$id\?> in the second example?
<p onclick="message(<?=$id;?>)">Test</p>
<script>
function message(id){
alert(id);
}
</script>
or
<p id="message">Test</p>
<script>
$("#message").click(function(){
alert(<?=$id;?>); //<---id
}
</script>
Your first sample is an outright syntax error. You'd be generating
<p onclick="message-7">Test</p>
Since anything inside the " for the onclick is Javascript code, you're doing "undefined variable minus 7".
If you want to store your 7, you either assign it to a JS variable, or pass it as a function argument:
<script>
var foo = <?php echo json_encode($id); ?>;
</script>
<!-- or you do -->
<p onclick="somefunction(<?php echo json_encode($id); ?>);">click me</p>
Note the use of json_encode(). It is not necessary in this case, but it's a good habit to get into. You should never be directly dumping a PHP variable into a javascript context with out it. Remember that the client browser will never see your PHP code. It'll just see whatever javascript you're generating, and if the PHP value contains illegal javascript, you'll just be producing a javascript syntax error and killing the entire block.
Question 1 is a duplicate of addEventListener vs onclick
Question 2: Yes, that should work :-)
I have a coldfusion page and I am very newbie in coldfusion. What I need to do is to insert the alert in between to see what is the time. In php I could close the php tags and enter the javascript tag and alert out the value. How would I do that in coldfusion? I have this
<cfset right_now=Now()>
<cfscript>
alert(#right_now#);
</cfscript>
But its not working.
thanks
<cfscript> is a Coldfusion tag for using the Coldfusion scripting language (aka CFScript). If you want to use Javascript, open a <script> tag like you would normally in HTML. You'll probably want to make sure it's inside a <cfoutput> tag if you want to use Coldfusion values within your javascript.
<cfset right_now = Now()>
<cfoutput>
<script type="text/javascript">
alert('#right_now#'); // don't forget you need to put quotes around strings in JS
</script>
</cfoutput>
You don't need to even use cfscript for this specific need. You could, for instance, do this:
<script type="text/javascript">
var currtime = new Date();
alert(currtime);
</script>
... Also a point to remember, you can't directly output HTML from within a <cfscript> tag. You can however get around this by calling a function from within a <cfscript> tag that can output the data for you.
Always remember the coldfusion begins and ends before anything else is executed: html, javaScript, sql, etc., so the javascript is getting an already formed code, which is CF instead of being hard coded.