why javascript doesnt run - javascript

I am learning JavaScript.
I've written this code, but it does not seem to run.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<script language="javascript" type="text/javascript">
function nav(){
try{
var str="<table border=1>";
for (var n in navigator){
str+="<tr><td>" + n + "</td></tr>";
}
str+="</table>
return("jetzt wird das Ding aufgelistet: <br/>" + str);
}catch(e){return(e);}
}
function writeit(){
document.write(nav());
}
</script>
<title>for learning purpos</title>
</head>
<body>
<form>
<script>
document.write(nav());
</script>
<p>press the button to get the properties in navigator</p>
<input type="button" id="btnNavigator" value="get" onclick="writeit();"/>
</form>
</body>
</html>

You haven't closed one string.
str+="</table>";

Best practice is to look into the console from developer tab, inspect page. Also on that tab on the bottom, you can try javascript code or even jQuery if you have the library added.
Your code it's wrong at line 13 you have Invalid or unexpected token because you didn't close the string and at line 30 nav it's not defined.
You can use this code:
function navBuild(n)
{
var tblS = "<table>";
var tblE = "</table>";
var strTrTd = "";
for (i=1; i<=n; i++)
{
strTrTd = strTrTd + "<tr><td>" + i + "</td></tr>";
}
var strAll = tblS + strTrTd + tblE;
console.log(strAll);
document.getElementById("contentBox").innerHTML = strAll;
}
And in your HTML you could use:
<input type="button" id="btnNavigator" value="get" onclick="navBuild(5);"/>
<div id="contentBox"></div>

Related

I get a red curve line when using f-string

def email_verification(email,password):
message = Mail(
from_email='hesheitaliabu#gmail.com',
to_emails=email,
subject='HEY BABYYYYYYYYYYYYYYYYYYY',
html_content=f"""
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">
<body>
<button onclick="myFunction()">Replace document</button>
<script>
function myFunction() {
window.location.href = "email_verify/" + email + "/" + password;
}
</script>
</body>
</html>
""")
Sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('MyAPI'))
Sg.send(message)
I have define a function called "email_verification" on FLASK to send the email with HTML design. I have used the f-string for the HTML content. But, I get a red curve line under the line of window.location.href = "email_verify/" + email + "/" + password;.
You need to escape literal braces {} in f-Strings in python.
You do this by doubling them. e.g.
def email_verification(email,password):
message = Mail(
from_email='hesheitaliabu#gmail.com',
to_emails=email,
subject='HEY BABYYYYYYYYYYYYYYYYYYY',
html_content=f"""
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">
<body>
<button onclick="myFunction()">Replace document</button>
<script>
function myFunction() {{
window.location.href = "email_verify/" + email + "/" + password;
}}
</script>
</body>
</html>
""")
Sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('MyAPI'))
Sg.send(message)
Notice here in the syntax highlighting how the body of myFunction is part of the f-String (green) and not a template variable (white) as in your question.
See https://realpython.com/python-f-strings/#braces

Why my jquery isn't displayed by browser?

so i am new to jq and i am trying to run this code but the browser doesn't show anything, tried to link the script library before ending tag of body.
Thanks!
<!DOCTYPE html>
<html>
enter code here
<head>
<meta charset="utf-8">
<title>Data Types</title>
<script src="jq/jquery-3.2.1.min.js"></script>
</head>
<body>
<script>
document.write(3.25 + 1000);
document.write('<br>' + 5 - 1000);
var1 = "Buna";
var2 = "Ziua";
document.write("<br>Concatenare siruri:" + var1 + var2 + '<br>');
a = true;
document.write(a);
var = fructe['mere', 'pere', 'caise'];
document.write('<br>' + fructe[1]);
</script>
</body>
</html>
You should fix this line:
var = fructe['mere', 'pere', 'caise'];
To
var fructe = ['mere', 'pere', 'caise'];

Unknown symbols displayed in czech words in javascript application

I have an unknown symbols displayed in my javascript application for try catch construction using czech language even when I use coding windows-1250. These symbols is displayed like question marks in diamond.
html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1250"/>
<title>Konstrukce Try/Catch</title>
<script type="text/javascript" src="number.js"></script>
</head>
<body>
<form name="formular" id="formular" action="#">
<div id="cisloDiv">Zadejte číslo v rozsahu 1 až 100: <input id="cislo" name="cislo"> <span id="informace"> </span></div>
<div><input id="odeslatFormular" type="submit"></div>
</form>
<script type="text/javascript">
function inicializuj() {
document.forms[0].onsubmit = function() { return zkontrolujFormular(this) };
}
window.onload = inicializuj;
</script>
</body>
</html>
javascript
function zkontrolujFormular() {
try {
var cislo = document.forms[0]["cislo"];
if (isNaN(cislo.value)) {
var chyba = new Array("Nejedná se o číslo",cislo);
throw chyba;
}
else if (cislo.value > 100) {
var chyba = new Array("Zadané číslo je větší jak 100",cislo);
throw chyba;
}
else if (cislo.value < 1) {
var chyba = new Array("Zadané číslo je menší jak 1",cislo);
throw chyba;
}
return true;
}
catch(objektVyjimky) {
var informace = document.getElementById("informace");
var textChyby = document.createTextNode(objektVyjimky[0]);
var novySpan = document.createElement("span");
novySpan.appendChild(textChyby);
novySpan.style.color = "#FF0000";
novySpan.style.fontWeight = "bold";
novySpan.setAttribute("id","informace");
var rodic = informace.parentNode;
rodic.replaceChild(novySpan,informace);
objektVyjimky[1].style.background = "#FF0000";
return false;
}
}
Have you tried using UTF-8 encoding?
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
or shorter:
<meta charset="utf-8" />
Nowadays using encoding other that UTF-8 is rather rare. You may need it in special circumstances, but whenever you can try to use UTF.
Now I have the solution, something was bad in my code because in solution (it is from learning material) it works.

Google Feed API for RSS is not showing current events

I am attempting to use Google Feed API to display three current event listings for our homepage. This is to circumvent some of the issues we are having with a third part calendar application.
However, with the feed limit set to 3, the only listings that will show up for me are from Jan. 18. Is there a way to make the code show only current or future events?
Thanks in advance for any help.
HTML
<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<meta charset="utf-8">
<title></title>
<script type="text/javascript" src="http://www.google.com/jsapi">
</script>
</head>
<body>
<div id="feed"></div>
</body>
</html>
JavaScript
google.load("feeds", "1");
var feedcontainer=document.getElementById("feed");
var feedurl="http://25livepub.collegenet.com/calendars/publishers-calendar-7.rss";
var feedlimit = 3;
var rssoutput = '';
function rssfeedsetup(){
var feedpointer=new google.feeds.Feed(feedurl);
feedpointer.setNumEntries(feedlimit) ;
feedpointer.load(displayfeed);
}
function displayfeed(result){
if (!result.error){
var thefeeds=result.feed.entries;
for (var i=0; i<thefeeds.length; i++){
var untrimContent = thefeeds[i].content;
var trimContent = untrimContent.split("<br>", 2);
rssoutput+="<div><a href='" + thefeeds[i].link + "'>" + thefeeds[i].title + "</a></div>" + trimContent;
feedcontainer.innerHTML=rssoutput;
}
} else {
feedcontainer.innerHTML = "Error Loading Events";
}
}
window.onload=function(){
rssfeedsetup();
};

converting html tags and the content to string

my question is I need to create a string from the result of event.content. Event.content returns me an entry including html tags. I can use it like container.innerHTML = event.content. I need the event.content as a string. I tried to do something like this:
var a = '' + event.content;
But it doesn't work. Here the result of event.content:
<img src="http://image.weather.com/web/common/wxicons/31/30.gif?12122006" alt="" />Partly Cloudy, and 84 ° F. For more details?
I can't convert this into string in javascript. Is it possible? I also tried String(event.content). Ok I put my whole code.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=ISO-8859-1">
<title>Hava Durumu</title>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("feeds", "1");
function havabas(){
var feed = new google.feeds.Feed("http://rss.weather.com/weather/rss/local/TUXX0014? cm_ven=LWO&cm_cat=rss&par=LWO_rss");
feed.load(function(result) {
if (!result.error) {
var entry = result.feed.entries[0];
var container = document.getElementById("weather");
var c = entry.content;
var regex = /<img.*?src="(.*?)".*?>.*?([0-9]+\s*°)/;
var results = regex.exec(entry.content);
var html = '<img src= ' + results[1] + ' alt="" /> ' + results[2];
container.innerHTML = html;
}
});
}
function initialize() {
havabas();
setInterval(function(){havabas()},2000);
}
google.setOnLoadCallback(initialize);
</script>
</head>
<body>
<div id="weather"></div>
</body>
</html>
Your regex is invalid. It does not match the string from entry.content. The reason for this is °. Change your code to this:
var regex = /<img.*?src="(.*?)".*?>.*?([0-9]+)/;
var results = regex.exec(entry.content);
var html = '<img src= ' + results[1] + ' alt="" /> ' + results[2] + ' °';
DEMO
Probably you forgot to escape either the sigle quotes or double quotes with a backlash? This is a valid string -
var string="<img src=\"http://image.weather.com/web/common/wxicons/31/30.gif?12122006\" alt=\"\" />Partly Cloudy, and 84 ° F. For more details?";
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<script>
function get1()
{
var i='<img src="http://image.weather.com/web/common/wxicons/31/30.gif?12122006" alt="" />Partly Cloudy, and 84 ° F. For more details?';
var j='\'' +i+ '\'';
alert(j);
}
</script>
<BODY onload='get1();'>
</BODY>
</HTML>
Please try this

Categories

Resources