Electron - Jquery isn't working [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
Hello,
I downloaded electron and installed it on webstorm in order to learn it. I also installed Jquery using the terminal and including in my scripts but jquery isn't working but it is recognized in my code (I haven't got any error).
My code :
const remote = require('electron').remote;
let window = remote.getCurrentWindow();
function exitGame() {
alert("test");
$('exit').click(() => {
alert("closing");
window.close();
})
}
exitGame();
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Electron Test</title>
<!-- RESET CSS -->
<link rel="stylesheet" type="text/css" href="css/reset.css">
<!-- ADDING CSS -->
<link rel="stylesheet" type="text/css" href="css/main_menu.css">
</head>
<body>
<div id="exit">Leave the game</div>
<!-- ADDING JAVASCRIPT -->
<script>
// You can also require other files to run in this process
require('./renderer.js');
require('./js/jquery.min.js');
require('./js/main_menu.js');
</script>
</body>
</html>
The jQuery alert is working but the click isn't.
Even if I replace the "exit" id by "body" it's not working...
Can you tell me what's wrong ?
Thank you very much, best regards.

exit is an id and not some element. So you need to use # before it.
$('#exit').click(() => {
alert("closing");
window.close();
})
Better way to check jQuer is wokring or not could be:
if(!jQuery)
{
alert("Not working!");
}

Alright, made it working by editing the tag with the code requiere jQuery.
Before Electron's base script tag, I added a tag with my jQuery. It seems that it wasn't loaded

Related

How do I change an elements css properties in jquery if it is an id? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
So I am currently try to change a css property of an element using jquery so that it will change once the page has loaded. For some reason though when trying to add either a css style or change its current property nothing happens?
What I am trying to achieve is the twitter feed at the bottom of this website:
http://www.footballkit.co.uk/
Here is my code:
<<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="style.css">
<script src="jquery-1.12.0.min.js"></script>
</head>
//Main Code Below:
<body>
<div id="dr">
<h1>Hello World</h1>
</div>
<script>
$(document).ready(function(){
$("#dr").css("top","400px");
}
</script>
</body>
</html>
You have a syntax error, missing ); at the end, always check your developer's console to check for javascript errors (f12 usually)
$(document).ready(function(){
$("#dr").css("top","400px");
}); //Syntax error missing );
You have syntax error. That is not something important.
I made a demo for you on jsfiddle
$(document).ready(function(){
$("#dr").css("top","400px");
});
And if top is not working, you have to check your style. Element with static position can't get position (top, left, right, bottom)
DEMO with style

Java Script external file linking to html [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have a problem with using an external *.js file in a *.html file.
What I want to achieve is to modify a div. No matter what I do I can't seem to make it working. Why isn't this working ?
document.getElementById("MyDiv").color = "red";
x.color = "blue";
MyDiv.color = "red";
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="JavaScript.js"></script>
<title>js</title>
</head>
<body>
<div id="MyDiv">12</div>
</body>
</html>
,
Try
document.getElementById("MyDiv").style.color = "red";
after the document loaded. Usage of jQuery would be helpful. With jQuery:
$(document).ready(function() {
document.getElementById("MyDiv").style.color = "red";
}
Or native: Put the script tag at the end of your body tag:
<script>
(function() {
document.getElementById("MyDiv").style.color = "red";
})();
</script>
Why cant you just include the Script in the <head>? If it is only that small piece then it should be no Problem.
If you are linking to an external file, be sure the link path is correct.
Code below will resolve your problem:
var MyDiv = document.getElementById("MyDiv");
MyDiv.style.color = "red";
MyDiv.innerHTML = "asdkjnasd";
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>js</title>
</head>
<body>
<div id="MyDiv">12</div>
</body>
</html>

Unable to access html elements by tag using Java script [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
My html is like this:
<!DOCTYPE html>
<html>
<head>
<title>
Event Handling
</title>
</head>
<body>
<h1> This is the chapter in which I am handling Events.The site you should visit is: </h1>
<b>here<b>
<script src="events.js"></src>
</body>
</html>
The javascript is like this:
var link = document.getElementsByTagName("a")[0];
link.onclick = MyEventHandler;
function MyEventHandler()
{
alert("ouch!");
return false;
}
It is not working, but using inling event handler I am doing it successfully. Please helop me.
You need to assign the event handler function, not the return value of calling that function.
link.onclick = MyEventHandler;
(Also note that you need to use the end tag </script> not </src>.)

Why is my button not doing anything? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am trying to use HTML to call a function in an external JavaScript file. The JavaScript file is called "javascript.js".
Here is my HTML code:
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="javascript.js">
</script>
<script
// Calling the Google Maps API
</script>
<script>
<!-- JavaScript to load Google Maps -->
</script>
</head>
<body>
<div class="content">
<div id="googleMap"></div> <!--don't have to worry about this -->
<div id="right_pane_results">hi</div> <!--don't have to worry about this -->
<div id="bottom_pane_options">
<button onclick="todaydate()">Try It</button>
</div>
</div>
</body>
...and my JavaScript code (something I got from the internet just to test):
function todaydate() {
var today_date=new Date()
var myyear=today_date.getYear()
var mymonth=today_date.getMonth() + 1
var mytoday=today_date.getDate()
document.write("<h1>"+myyear+"/"+mymonth+"/"+mytoday+"/h1">)
}
On my webpage that I'm running locally, the button is showing, but nothing happens when I click on it. Is it something to do with my code?
Thanks in advance,
Josh
There are 2 mistakes inside the function. The > needs to be inside the quotes and there needs to be a < before the /h1.
Replace this line:
document.write("<h1>"+myyear+"/"+mymonth+"/"+mytoday+"/h1">)
with this
document.write("<h1>"+myyear+"/"+mymonth+"/"+mytoday+"</h1>")
and it should work.
I guess this has to do with the syntax error you have:
document.write("<h1>"+myyear+"/"+mymonth+"/"+mytoday+"</h1>")
note how the last quote is after the > (and you also forgot the < for the closing h1)
document.write("<h1>"+myyear+"/"+mymonth+"/"+mytoday+"</h1>")
This works!
As other answer mention syntax issue is in your document.write statement.
Beside this use getFullYear() to get year as getYear() is Deprecated.
some other practices to make your code cleaner:
Use semi-colan at end of statement
Use a good naming convention
Here is Demo
function getTodayDate() {
var todayDate=new Date();
var myYear=todayDate.getFullYear();
var myMonth=todayDate.getMonth() + 1;
var myDay=todayDate.getDate();
document.write("<h1>"+myYear+"/"+myMonth+"/"+myDay+"</h1>");
}

Error about dynamic countdown redirect [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I have an javascript to redirect to another page after countdown 10 seconds, but it doesn't work. Can anyone help to fix the problem?
<body>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type ="text/javascript" src ="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var number = 10;
var url = 'http://localhost/rms/index.php/';
function countdown() {
setTimeout(countdown,1000);
$('#title1').html("Redirecting in " + number + " seconds");
number --;
if(number<0) {
window.location = url;
number = 0;
}
}
countdown();
});
</script>
</head>
<body>
<div id="title1"></div>
</body>
As everybody wrote in the comments, it should work. Try to replace
<script type ="text/javascript" src ="jquery.js"></script>
with this:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script>
In this case you don't have to worry about your jquery.js file, which could be missing or corrupted.
I have one note to your code. Are you aware of that you call window.location = url; two times?
If its just a regular countdown with redirect I will put the setTimeout call in if statement which will prevent calling it twice.

Categories