Artifacts Gallery Guilds Search Wiki Login Register

Welcome, Guest. Please login or register. - Thinking of joining?
a Summer night - @140.51
Activity rating: Four Stars Posts & Arts: 63/1k.beats Random | Recent Posts | Guild Recents
News: It's only life! :dog: Guild Events: 9: information

+  MelonLand Forum
|-+  Life & The Web
| |-+  ✁ ∙ Web Crafting
| | |-+  ☔︎ ∙ I need Help!
| | | |-+  What's wrong with my javascript?


« previous next »
Pages: [1] Print Embed
Author Topic: What's wrong with my javascript?  (Read 222 times)
Noah_S
Sr. Member ⚓︎
****
View Profile WWWArt


Noah. Earth. I make website. I.. I.. Noah. Earth.
⛺︎ My Room
iMood: Noah_S
RSS: RSS

Guild Memberships:
Artifacts:
Visited on Melon's 10th Anniversary!I got robbed by Dan Q on Melonland!I met Dan Q on Melonland!A coin!EnbyAce
« on: a Summer day » Embed

I'm trying to figure out some javascript in the w3school experiment box. So far I have

Code
<!DOCTYPE html>
<html>
<body>

<h2>Get the current URL</h2><span id="demo"></span>

<script>
if (window.location.href == https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_get_url) {
let text = "Yes";
} else {
let text = "No";
}

document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

It's not displaying anything when I run it. I can't figure out what's wrong. I've been trying different combinations, checking my ";"s, everything I can think of. The code doesn't even look complicated, but javascript is my weakest language and I can't figure out why this code refuses to cooperate. :dunno:

Logged

perfectionist_better.gifweb_designer.gifspeed_of_time.gifbutton290.pngmy_dream_wedding.gifClippy beats AI

Artifact Swap: Large watercraftGrowing ballWan wan!Melonland has encountered a bugMerry ChristmasMessage Buddy
Dan Q
Hero Member ⚓︎
*****
View Profile WWWArt


I have no idea what I am doing
⛺︎ My Room
RSS: RSS

Guild Memberships:
Artifacts:
Dan Q Cruisin'I DIDN'T meet Dan Q on Melonland!Visited on Melon's 10th Anniversary!
« Reply #1 on: a Summer day » Embed

Looks like a syntax error to me. https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_get_url is a string, so it should be wrapped in some variety of quotation marks.

Without the quotation marks, it looks like you're asking whether window.location.href == https:. And then the // starts a comment. This is why - if you open your browser's debug tools - you'll see a syntax error complaining about a missing ) - you've commented out the end of the line which includes it!

Once you fix that, the next problem is that you're declaring (let) your variable inside a block ({ ... }). Which means that it won't exist outside that block.

Once you've fixed both of those problems, you get this, which seems to work:


Code
<!DOCTYPE html>
<html>
<body>

<h2>Get the current URL</h2><span id="demo"></span>
 
<script>
let text; // declare the variable here!
if (window.location.href == 'https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_get_url') {
  text = "Yes";
} else {
  text = "No";
}

document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

Logged

https://danq.me/_q26t/badges/dan-q-88x31-lighter.gif https://danq.me/_q26t/badges/dan-q-88x31-peekaboo-scroller.gif https://beige-buttons.danq.dev/beige-buttons-88x31.gif https://embed-html.danq.dev/embed-html-88x31.gif

Artifact Swap: PolyamorousI met Dan Q on Melonland!Joined 2025!
Noah_S
Sr. Member ⚓︎
****
View Profile WWWArt


Noah. Earth. I make website. I.. I.. Noah. Earth.
⛺︎ My Room
iMood: Noah_S
RSS: RSS

Guild Memberships:
Artifacts:
Visited on Melon's 10th Anniversary!I got robbed by Dan Q on Melonland!I met Dan Q on Melonland!A coin!EnbyAce
« Reply #2 on: a Summer day » Embed

Thank you.

I'm so used to PHP variables existing.

Logged

perfectionist_better.gifweb_designer.gifspeed_of_time.gifbutton290.pngmy_dream_wedding.gifClippy beats AI

Artifact Swap: Large watercraftGrowing ballWan wan!Melonland has encountered a bugMerry ChristmasMessage Buddy
Dan Q
Hero Member ⚓︎
*****
View Profile WWWArt


I have no idea what I am doing
⛺︎ My Room
RSS: RSS

Guild Memberships:
Artifacts:
Dan Q Cruisin'I DIDN'T meet Dan Q on Melonland!Visited on Melon's 10th Anniversary!
« Reply #3 on: a Summer day » Embed

So:

Classic JavaScript variable use the var keyword. And they're globally scoped. But you shouldn't be using them, because... well... global-scoping introduces terrible ways to shoot yourself in the foot. Modern JavaScript uses let and const, which not only help by ensuring that it's clear whether a variable will ever change... they also help by limiting the scope of the variable to the function or block in which they're defined. Generally much-cleaner programming.

PHP also has variable scoping. A variable declared in an object or function isn't available to a different object or function unless you pass it as a parameter (preferred) or use the 'global' keyword to share it between them. Click run code to run this example; then change the 'global' line so that one of the top-level variables doesn't get included in the function and try again to see a "Warning: Undefined variable".

So yeah: both (modern) JS and (normal) PHP do variable scoping in a similar way. The big difference is that JS scopes to "function OR block", where as PHP scopes to "function". If you declare a variable inside a loop in PHP, it's available outside the loop; but in JS it isn't. But both are scoped to the functions that contain them. They're more-similar than you'd think!

Logged

https://danq.me/_q26t/badges/dan-q-88x31-lighter.gif https://danq.me/_q26t/badges/dan-q-88x31-peekaboo-scroller.gif https://beige-buttons.danq.dev/beige-buttons-88x31.gif https://embed-html.danq.dev/embed-html-88x31.gif

Artifact Swap: PolyamorousI met Dan Q on Melonland!Joined 2025!
Pages: [1] Print Embed 
« previous next »
 

Melonking.Net © Always and ever was! SMF 2.0.19 | SMF © 2021 | Privacy Notice | Send Feedback | Supporters ♥ Forum Guide | Rules | RSS | WAP | Mobile


MelonLand Badges and Other Melon Sites!

MelonLand Project! Visit the MelonLand Forum! Support the Forum
Visit Melonking.Net! Visit the Gif Gallery! Pixel Sea TamaNOTchi
@000 MelonLand Editor Recent Edits Tenement Arcade Want to Login or Join ? Forum Art Hub Chat Webring Web Guides Graphics Catalogue Wiki Newsletters Image Stream
Melon's Sites TamaNotchi Textures PixelSea GifyPet MoMG Ozwomp Online Loom Videos Leaky Webring Melonking
Tools Melon Software ArtHub Embed Maker ML Passports
Outlinks Frutiger Aero Forum Onio Club m15o's Web Services 32Bit Cafe iMood Webrings Internet Phone Book HTML Energy Declarations Hackers & Designers
Zap!
Minecraft: Online
Join: craft.melonking.net