Home Events! Entrance Everyone Wiki Search Login Register

Welcome, Guest. Please login or register. - Thinking of joining the forum??
October 18, 2024 - @470.36 (what is this?)
Forum activity rating: Three Stars Posts: 27/1k.beats Unread Topics | Unread Replies | Own Posts | Own Topics | Random Topic | Recent Posts
News: :ozwomp: Reminder: Forum messages stay readable for years! Keep yours high quality! :ozwomp:
Halloween Topics! Gfx + Costumes, Decor + Crafts, Prep, Horror Movies, Spooky Songs! ~ E-Zine #2 is out now!

+  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 675 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)

Theme Poll - Do you like the forum theme?

Started by MelooonBoard ⛄︎ ∙ Forum Info & Questions

Replies: 20
Views: 9397
Last post October 17, 2024 @830.53
by ヤーナム
HELP: troubles in implementing iframe to my existing layout

Started by morrysillusionBoard ☔︎ ∙ I need Help!

Replies: 8
Views: 2148
Last post June 07, 2022 @522.55
by Melooon
MelonLand Vivaldi Theme ♡

Started by MelooonBoard ⚛︎ ∙ MelonLand Projects

Replies: 6
Views: 2219
Last post March 05, 2023 @376.64
by doubleincision

Melonking.Net © Always and ever was! SMF 2.0.19 | SMF © 2021, Simple Machines | Terms and Policies Forum Guide | Rules | RSS | WAP2


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