Artifacts Gallery Guilds Search Wiki Login Register

Welcome, Guest. Please login or register. - Thinking of joining?
a Summer night - @81.67
Activity rating: Four Stars Posts & Arts: 63/1k.beats Random | Recent Posts | Guild Recents
News: :seal: Thank you for today! :seal: Guild Events: 9: information

+  MelonLand Forum
|-+  Life & The Web
| |-+  ✁ ∙ Web Crafting
| | |-+  ☔︎ ∙ I need Help!
| | | |-+  Getting the current pathname from a iframe in js


« previous next »
Pages: [1] Print Embed
Author Topic: Getting the current pathname from a iframe in js  (Read 1343 times)
Memory
Guest

« on: a Spring night » Embed

I have this piece of JS at the very end of the HTML <body> that look at an iframe for the current page it's on, I'm doing this as a way to tell the viewer what music is playing, however it seems that the pathname just returns "blank".

Code
var music_credits = document.getElementById("music-credits")
var mainframe = document.getElementById("mainframe")
var mf_page = mainframe.contentWindow.location.pathname

mainframe.addEventListener("load", function() {
  console.log(mf_page)
  if (mf_page.includes("home.html")) {
     music_credits.textContent = "Home (Slowed & Reverbed)"
  } else if (mf_page.includes("bulletin.html")) {
     music_credits.textContent = "kensune by nicopatty"
  } else if (mf_page.includes("me.html")) {
     music_credits.textContent = "Stickerbush Symphony"
  } else {
     music_credits.textContent = "null"
  }
});

Logged
Melooon
Hero Member ⚓︎
*****
View Profile WWWArt


So many stars!
⛺︎ My Room
SpaceHey: Friend Me!
StatusCafe: melon
iMood: Melonking
Itch.io: My Games
RSS: RSS

Guild Memberships:
Artifacts:
Bug!You're a Star!Flinstone VitaminI got robbed by Dan Q on Melonland!Always working hard!Known Apple shill
« Reply #1 on: a Spring day » Embed

I suspect it’s because you are getting your pathname before the iframe has actually loaded, you need to put the mf_page var inside the load event ^^

Logged


everything lost will be recovered, when you drift into the arms of the undiscovered

Artifact Swap: Red TulipLurbyPlank
Memory
Guest

« Reply #2 on: a Spring day » Embed

I suspect it’s because you are getting your pathname before the iframe has actually loaded, you need to put the mf_page var inside the load event ^^

This solved the problem!...

Although one thing that I will mention is that the js breaks when you try and run the site as a local file on your computer due to cross-origin restrictions. It also breaks your iframe link script as well, have you found a work around for that?

Logged
Melooon
Hero Member ⚓︎
*****
View Profile WWWArt


So many stars!
⛺︎ My Room
SpaceHey: Friend Me!
StatusCafe: melon
iMood: Melonking
Itch.io: My Games
RSS: RSS

Guild Memberships:
Artifacts:
Bug!You're a Star!Flinstone VitaminI got robbed by Dan Q on Melonland!Always working hard!Known Apple shill
« Reply #3 on: a Spring day » Embed

It also breaks your iframe link script as well, have you found a workaround for that?
Yes, use a local web server when developing JS on your computer :ok: Like Live Edit in VS Code or if you have Python installed (its preinstalled on macs and many linux distros) you can run this command to setup a webserver at your current directory:
Code
python3 -m http.server 8000
Then visit http://localhost:8000  :ha:

Logged


everything lost will be recovered, when you drift into the arms of the undiscovered

Artifact Swap: Red TulipLurbyPlank
Memory
Guest

« Reply #4 on: a Spring night » Embed

Yes, use a local web server when developing JS on your computer :ok: Like Live Edit in VS Code or if you have Python installed (its preinstalled on macs and many linux distros) you can run this command to setup a webserver at your current directory:
Code
python3 -m http.server 8000
Then visit http://localhost:8000  :ha:

I didn't know python could do that! Thanks for this! :ha:

Logged
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