Home Events! Entrance Everyone Wiki Search Login Register

Welcome, Guest. Please login or register. - Thinking of joining the forum??
November 21, 2024 - @732.93 (what is this?)
Forum activity rating: Three Stars Posts: 35/1k.beats Unread Topics | Unread Replies | My Stuff | Random Topic | Recent Posts    Start New Topic
News: :dive: Are u having fun?? :dive:

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


« previous next »
Pages: [1] Print
Author Topic: Getting the current pathname from a iframe in js  (Read 696 times)
Icey!
Sr. Member ⚓︎
****


Icey/Iceys

⛺︎ My Room

View Profile WWW

First 1000 Members!Pro Bug Finder!OG! Joined 2021!High Speed Ozwomp!
« on: May 07, 2024 @214.11 »

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



:ozwomp: my beloved

Melooon
Hero Member ⚓︎
*****


So many stars!

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

View Profile WWW

Thanks for being rad!a puppy for your travelsAlways My PalFirst 1000 Members!spring 2023!Squirtle!!!!MIDI WarriorMIDI Warrior1234 Posts!OzspeckCool Dude AwardRising Star of the Web AwardMessage BuddyPocket Icelogist!OG! Joined 2021!...
« Reply #1 on: May 07, 2024 @415.64 »

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
Icey!
Sr. Member ⚓︎
****


Icey/Iceys

⛺︎ My Room

View Profile WWW

First 1000 Members!Pro Bug Finder!OG! Joined 2021!High Speed Ozwomp!
« Reply #2 on: May 07, 2024 @704.84 »

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



:ozwomp: my beloved

Melooon
Hero Member ⚓︎
*****


So many stars!

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

View Profile WWW

Thanks for being rad!a puppy for your travelsAlways My PalFirst 1000 Members!spring 2023!Squirtle!!!!MIDI WarriorMIDI Warrior1234 Posts!OzspeckCool Dude AwardRising Star of the Web AwardMessage BuddyPocket Icelogist!OG! Joined 2021!...
« Reply #3 on: May 07, 2024 @739.70 »

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
Icey!
Sr. Member ⚓︎
****


Icey/Iceys

⛺︎ My Room

View Profile WWW

First 1000 Members!Pro Bug Finder!OG! Joined 2021!High Speed Ozwomp!
« Reply #4 on: May 07, 2024 @806.04 »

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



:ozwomp: my beloved

Pages: [1] Print 
« previous next »
 

Vaguely similar topics! (3)

HELP: troubles in implementing iframe to my existing layout

Started by morrysillusionBoard ☔︎ ∙ I need Help!

Replies: 8
Views: 2180
Last post June 07, 2022 @522.55
by Melooon
Do you have a current generation console?

Started by MemoryBoard ♖ ∙ Video Games

Replies: 26
Views: 3164
Last post December 19, 2023 @757.82
by georgemoody
FromSoftware / Soulsborne Games

Started by sigBoard ♖ ∙ Video Games

Replies: 5
Views: 954
Last post March 09, 2023 @990.24
by Memory

Melonking.Net © Always and ever was! SMF 2.0.19 | SMF © 2021, Simple Machines | Terms and Policies 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