<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h2> why ${variableName} is in consider as undefined in jsp page
<script>
let x="x";
console.log(`${x} is the variable`)
</script>
</body>
</html>
${} is consider as undefined in jsp pages.
Please tell me how it is coming as not expexted.
Thank You!
The fix is to escape the ${myVar} with a backslash so that JSP ignores it:
//my.jsp
<script>
var n = "Dave";
var s = `Hello \${n}`; //note the backslash
</script>
Related
I want to use EditorJS in my project. So I have created an HTML file after reading the documentation. Here is the file called index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home</title>
</head>
<body>
<div id="editorjs"></div>
<script src="https://cdn.jsdelivr.net/npm/#editorjs/editorjs#latest"></script>
<script>
import EditorJS from '#editorjs/editorjs'
const editor = new EditorJS('editorjs')
</script>
</body>
</html>
After opening the file into the browser, EditorJS is not showing up and this message is printed in the console- Uncaught SyntaxError: Cannot use import statement outside a module.
How to fix this problem?
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://cdn.jsdelivr.net/npm/#editorjs/editorjs#latest"></script>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home</title>
</head>
<body>
<div id="editorjs"></div>
<script>
const editor = new EditorJS({
autofocus: true
});
</script>
</body>
</html>
When you add a script tag in your HTML file, you don't need to import the library again you can just start using it!
The script isn't a type module, use:
<script type="module">
This question already has answers here:
avoid to escape a special characters in javascript
(5 answers)
Closed 1 year ago.
I want my text to be treated literally. When I write for example "\nraw\t" I want it to be displayed exactly like this - "\nraw\t", not "raw" when I use textContent or createTextNode.
Here is an exemplary HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Raw, ok</title>
</head>
<body>
<div id="first"></div>
<div id="second">\nRaw\t</div>
<script>
document.querySelector("#first").textContent = "\nRaw\t";
</script>
</body>
</html>
In div with id "first" I get "Raw" without "\n" and "\t". (This is the unwanted result.)
I'm satisfied with what String.raw`\nRaw\t` returns, but unfortunately, this method is not supported by a few browsers including Opera, so I cannot use it. Any suggestions would be great.
EDIT:
Okay, maybe I should have given more details about it and not try to simplify my actual problem. The thing is - it will not always be "\nRaw\t". It can be anything my user provides in a form - so text with no special characters, text with only special characters, anything they want, so I need this solution to be flexible.
If you don't want the string to have the two quotes at the beginning and end like the answer here: Javascript - How to show escape characters in a string?, just use the splice method to cut them away.
const str = JSON.stringify("\nRaw\t")
const cutStr = str.slice(1,str.length-1)
document.querySelector("#p").innerText = cutStr;
<div id="p"><div>
You can use JSON.stringify and then remove the first and last characters (the quotes) using String#slice.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Raw, ok</title>
</head>
<body>
<div id="first"></div>
<div id="second">\nRaw\t</div>
<script>
document.querySelector("#first").textContent = JSON.stringify("\nRaw\t").slice(1, -1);
</script>
</body>
</html>
You can escape with a backslash \
Or use JSON.stringify with slice to remove quotes as suggested by others.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Raw, ok</title>
</head>
<body>
<div id="first"></div>
<div id="second">\nRaw\t</div>
<script>
document.querySelector("#first").textContent = JSON.stringify("\nRaw\t").slice(1, -1);
</script>
</body>
</html>
This question already has answers here:
What do querySelectorAll and getElementsBy* methods return?
(12 answers)
Closed 1 year ago.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="top">surprise</div>
<script>
document.getElementsByClassName("top").style.color ="red";
</script>
</body>
</html>
I don't know why this is not working, hoping to have a good explanation .
You need to access style property on element. document.getElementsByClassName("top") will return you an HTMLCollection.
you need to update your script:
document.getElementsByClassName("top")[0].style.color ="red";
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="top">surprise</div>
<script>
document.getElementsByClassName("top")[0].style.color ="red";
</script>
</body>
</html>
If I had a markup like this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="file.js"></script>
<title>Document</title>
</head>
<body>
<h1>This is a header</h1>
<div id='container'>
</div>
</body>
</html>
and a JavaScript file associated with it like this:
const newButton = document.createElement('button');
newButton.innerText='Hey!';
document.getElementById('container').appendChild('newButton');
Why do I need to fetch div by either it's Id or by a referencing it to a variable via querySelector(), but I can simply reference body by something like:
document.body.appendChild('newButton');
This question already has answers here:
JavaScript not working on external file
(4 answers)
Closed 5 years ago.
i have 2 files....one html and one js....
html code:
<!DOCTYPE html
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="text/javascript" src="external.js"></script>
<title> Sign In And Registration Page </title>
</head>
<body>
<div id="headerTag">
</div>
///codes here....
</body>
</html>
js code:
some functions here performed on click operation.....
function onClickOperation ()
{
///here codes..
}
problem is that the functions are not being called...when i put the same js code in the html file directly, it works....what do i have to do to load those functions from separate js file?
external.js should have the code called after DOM has finished loading like so
document.addEventListener('DOMContentLoaded',function(){
// code here
});
or it should be included inside the body tag below the DOM elements it should interact with
Try wrapping the script in the body and just before the </body> tag:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title> Sign In And Registration Page </title>
</head>
<body>
<div id="headerTag">
</div>
///codes here....
<script type="text/javascript" src="external.js"></script>
</body>
</html>