Accessing an iframe document (contentWindow) - JavaScript Tutorial

Поделиться
HTML-код
  • Опубликовано: 20 сен 2024
  • In this video we take a look at the contentWindow and contentDocument properties of an embedded iframe - it allows access to the underlying "window" and "document" objects of the HTML page.
    Support me on Patreon:
    / dcode - with enough funding I plan to develop a website of some sort with a new developer experience!
    For your reference, check this out:
    developer.mozi...
    Follow me on Twitter @dcode!
    If this video helped you out and you'd like to see more, make sure to leave a like and subscribe to dcode!
    #dcode #javascript #webdev

Комментарии • 94

  • @dcode-software
    @dcode-software  2 года назад +1

    *RELATED VIDEO: SEND DATA BETWEEN TABS IN JAVASCRIPT*
    ruclips.net/video/wYcvzLFHFN0/видео.html
    🏷 *THE ULTIMATE JAVASCRIPT DOM CRASH COURSE* 👇
    www.udemy.com/course/the-ultimate-javascript-dom-crash-course/?referralCode=DC343E5C8ED163F337E1

    • @CodingMation
      @CodingMation 8 месяцев назад

      Bro I literally too confusing how to change light mode to dark. Because I make a nav bar for every page and button who change mode but when I used then it not changed so I used this method to send data I thought that it's work with local storage data without reload website.

  • @christianhelwig
    @christianhelwig 3 года назад +5

    Thank you!!! You helped me figure something out I was working on for four hours. This enabled me to understand multiple codes I was working with. Thank you!

  • @leopoldosantiago7027
    @leopoldosantiago7027 2 года назад +6

    Meu amigo gringo, muito obrigado! Sua solução é simples e fácil de implantar. Parabéns!

  • @TECHINSPEC
    @TECHINSPEC 3 года назад +29

    This is for the Same-origin Iframe only... You can't access from the different domain using the method explained in this video

    • @saurabhkum2050
      @saurabhkum2050 3 года назад +7

      Is there any way to do the same for different domains ?

    • @marcelo.victor
      @marcelo.victor 2 года назад +1

      Just use postMessage with all origins (*) and event listener to receive the message and you are all set.

    • @badmuskaybee5111
      @badmuskaybee5111 2 года назад

      @@marcelo.victor Can you clarify
      ?

    • @marcelo.victor
      @marcelo.victor 2 года назад +1

      @@badmuskaybee5111 on the child you add a event listener on page load to get this page height (document.body.scrollHeight) and pass it via postMessage (accepting all URL origins using * in message) to the parent page, that have a script with a window event listener to get that message and then apply that height passed to the style.

  • @omarzaky646
    @omarzaky646 2 года назад

    You are the best javascript programmer in the world. Thank you very much 🌹

  • @heatvisuals
    @heatvisuals 5 лет назад +9

    I like the typing sound effects. Sounds real af

    • @rezaz7167
      @rezaz7167 4 года назад +3

      it might be real, I think its a mechanical keyboard

    • @_.sunnyraj._
      @_.sunnyraj._ 4 года назад +1

      Fool

  • @hessiride1729
    @hessiride1729 3 года назад +7

    This example shows that will only work for local pages however if you link the to another link it will be dom exception for x-frame error

  • @Tmmy22
    @Tmmy22 2 года назад +9

    What if the origin of the is at a different origin, i.e. not local to the page? Is something similar possible that yeilds the same outcomes?

  • @newcomer4220
    @newcomer4220 3 года назад

    This channel has solution for almost every problem

    • @Hangoutapp
      @Hangoutapp 3 года назад

      An that's why I love the channel

  • @arnab1prince
    @arnab1prince 4 года назад +1

    Perfect, to the point video.. Thanks

  • @wowwwww1993
    @wowwwww1993 3 года назад +5

    Thank you for this great video. Can you post a version with an onload-execution? As soon as the script is loaded, some JS-code should be executed.

  • @vivianeasaraiva80
    @vivianeasaraiva80 6 месяцев назад

    Hi, please could you do a video showing how to use Javascript to change content inside every 5s? That would be very helpfull

  • @valentinagajic7559
    @valentinagajic7559 4 года назад +16

    This is ok, when pages are from same source,but how to do that if we embed in some other website? Can we manipulating with objects of that?

    • @guilhermezanetti1046
      @guilhermezanetti1046 4 года назад +1

      how?

    • @pradeepkumarrana5651
      @pradeepkumarrana5651 3 года назад

      Anyone Reply?

    • @abh1yan
      @abh1yan 3 года назад

      @@pradeepkumarrana5651 it's not possible

    • @Snoo29293
      @Snoo29293 3 года назад

      There is the pointer-events CSS property you can use, however, when used, the s are ignored by all the mouse events, meaning your window mousemove events will be detected even when hovered over the , but you will not be able to interact with the anymore, this isn't the perfect solution, but in certain situations this may be just good enough to solve your problems, if you want for example to be able to drag an element around your page and want to prevent s from canceling your mousemove event you can change the pointer-events property for s every time the element is being dragged and change it back after it is dropped.

    • @kirua_h
      @kirua_h 3 года назад

      This is not working for me. Its saying "blocked a frame with origin null from accessing a cross origin frame" even though both the files are in the same folder

  • @mr.c7411
    @mr.c7411 3 года назад

    Great video man

  • @celsoferreira8625
    @celsoferreira8625 Год назад

    Thanks man. That´s great.

  • @murtazamohammadi9370
    @murtazamohammadi9370 2 года назад +1

    I have one question about Iframe: I displayed an external website in my angular project, inside external website I have a link to open pdf. if I click on that link, it will be opened in a new tab. I want to open it inside the . how can I do that. I would be very happy hearing back from you thanks

  • @michaelcrossley4716
    @michaelcrossley4716 Год назад

    big help! Thanks.

  • @franktorrejonhuarcaya4370
    @franktorrejonhuarcaya4370 4 года назад

    Thank you! it was very helpful.

  • @cmtg4471
    @cmtg4471 3 года назад

    Nice content by the way

  • @colindante5164
    @colindante5164 2 года назад

    Awesome!!!! Thankyou much)))

  • @skimran22
    @skimran22 5 лет назад +1

    Thanks it’s very helpful

  • @nishilsuthar2749
    @nishilsuthar2749 4 месяца назад

    But what can I do this if the component is comming from another origin

  • @manjindersingh-gj4ww
    @manjindersingh-gj4ww 5 лет назад

    Best one I found

  • @ilyaklimov3135
    @ilyaklimov3135 4 года назад +1

    Thank you!

  • @vd-bro9985
    @vd-bro9985 Год назад

    great video

  • @vicente5907
    @vicente5907 4 года назад +1

    Hello, tnx for this video is very helpful. I just want to ask if is possible to do the same staff but with the button inside the ? for example i want to push the button and then close the do a z-index in order to hidde it.
    Kind regards.

  • @abusalahamusazangi6507
    @abusalahamusazangi6507 10 месяцев назад

    How to insert a script within a head tag of an iFrame if the iFrame has no id attributes?

  • @thefreshweb
    @thefreshweb 4 года назад

    thank you very much, very much, very much ❤️💖💜💙

  • @bikramchettri9405
    @bikramchettri9405 5 лет назад +2

    Can you show how to use scrollHeight properties of an element?

  • @user-888azim-97
    @user-888azim-97 3 года назад +1

    could you also make video for embed pls?

  • @thelostman5625
    @thelostman5625 2 года назад

    *Question:*
    How can I open external application (on mobile) using JavaScript (create open-in-app feature)?

  • @TBrianOnline
    @TBrianOnline Год назад

    Now please make it to work in React, which has a different dynamic of loading window.

  • @rezaz7167
    @rezaz7167 4 года назад +3

    .contentDocument is null, do you guys know why ?

  • @stig6339
    @stig6339 4 месяца назад

    how do i change the file inside an to another at runtime

  • @vmars316
    @vmars316 3 года назад

    Do I need the .css to make it run error free ?
    const Document = myIframe.contentDocument;
    Why does this statemment retern null ?:
    console.log(Document) // ? = null
    WHy does this starement give me this error:
    Uncaught TypeError: Cannot read property 'querySelector' of null
    Document.body.style.backgroundColor = lightgreen ;
    I have typed out your code many times , but it always gives errors .
    Can I get a copy of the source code .
    Thanks

  • @zqgAFf
    @zqgAFf Год назад

    I am a bit curious how to get the console content from this and show it somewhere else in the web browser not in the developer tools

  • @vicente5907
    @vicente5907 4 года назад +1

    Uncaught DOMException: Blocked a frame with origin "null" from accessing a cross-origin frame.
    i get this error when i do a console.log(myIframe)

    • @forallah7615
      @forallah7615 4 года назад

      I had the same error ..Take a look at this :
      stackoverflow.com/questions/29983786/blocked-a-frame-of-origin-null-from-accessing-a-cross-origin-frame-chrome

    • @TECHINSPEC
      @TECHINSPEC 3 года назад

      This is for the Same-origin Iframe only... You can't access from different domain using the method explained in this video

  • @shashikantvaidya5252
    @shashikantvaidya5252 4 года назад +1

    Hello sir my question is i have Iframe and inside i have Pdf file i want to get the position of Scroll bar of how to get

    • @vmars316
      @vmars316 3 года назад

      Sounds interesting , can you share your code , Thanks

  • @nyctophillia2292
    @nyctophillia2292 2 года назад

    why is the nested document not listening to events?

  • @kedarsatwik6938
    @kedarsatwik6938 3 года назад

    hello, video is very nice and useful,
    I have a button inside , and nav bar ouside , how can we write a function so that when we click button nav bar should disappear

  • @zakimdh1070
    @zakimdh1070 2 года назад

    Hello , can you make popup work inside box

  • @satishganai3942
    @satishganai3942 5 лет назад +1

    how to access cross-origin external website like google.com inside . I am getting cross-origin error. Please help

    • @dcode-software
      @dcode-software  5 лет назад +1

      It's not an error, it's a restriction. Looks like Google doesn't want people embedding their website in an IFrame. Google has requested this and Chrome is obliging

    • @vicente5907
      @vicente5907 4 года назад +1

      @@dcode-software fak google!

  • @raymytech
    @raymytech 3 года назад

    How can I load a specific table or div only from another website?

  • @leandrmiklashevich297
    @leandrmiklashevich297 4 года назад

    Ok. I have this situation:
    I have a page with (same origin). I make a click on a button within . How to handle it in my parent page? How to send some kind of data from to parent page? Thank you.

    • @justluthra
      @justluthra 4 года назад

      if you founded solution then please help me, I am stuck on same problem

  • @staceyloulouse
    @staceyloulouse 3 года назад

    seems life it would be more efficient to use the target attribute to place an , but thanks for sharing.

  • @daniels3401
    @daniels3401 4 года назад

    I think I know the answer to this already but theres not any way to actually do this cross origin is there? All my leads are telling me no

  • @mdridoy-ef2pw
    @mdridoy-ef2pw 2 года назад

    I have a textarea and an .
    I want whatever user types in the textarea to be executed inside the as a script.
    for example if user types console.log('hello world'), I want this code to be executed inside the as a script.
    Is there any way to do it.
    Maybe adding the text as a script inside the or something?

  • @buddyvikas9285
    @buddyvikas9285 3 года назад

    Sir can you please tell me how to prevent to make browser history?

  • @buenito
    @buenito 4 года назад

    problem in ie8, you solved?

  • @bsk8643
    @bsk8643 2 года назад

    Can you tell how to handle in case of below html...
    #document

    #document
    .....and so on...
    In this case how do I find a specific elemrnt in nth document inside nth ?
    Can you please reproduce this and help me to understand 🙏

  • @amatuluzma3828
    @amatuluzma3828 3 года назад

    can I save content to database and then retrieve it?

  • @mohammedhissham8719
    @mohammedhissham8719 4 года назад

    wht if button is in another

  • @ramureddy2500
    @ramureddy2500 4 года назад

    it is posible Accessing an document in angualr 6 ?

  • @vicovdeventuin
    @vicovdeventuin 4 года назад

    Thanks! Is it also possible to pause and start an embedded video from youtube that is running in an iFrame?

    • @kevinb6568
      @kevinb6568 3 года назад +1

      Yes it is. For example:

    • @keppu2661
      @keppu2661 2 года назад

      @@kevinb6568 is it also possible to use this one on video widgets of elementor?

  • @ashutoshmehere5915
    @ashutoshmehere5915 3 года назад

    Content security police error sir

  • @BilalHassan-wm5ev
    @BilalHassan-wm5ev 3 года назад

    hoe to remove div from ?

  • @igu642
    @igu642 4 месяца назад

    ❤❤

  •  4 года назад

    nice

  • @varunvc532
    @varunvc532 3 года назад

    guy's name is DOM damn

  • @rooiboquality6603
    @rooiboquality6603 4 года назад

    Can you post the source code somewhere?

  • @kushkumar2878
    @kushkumar2878 5 лет назад

    I am getting below error while accessing the with its id
    SecurityError: Permission denied to access property "document" on cross-origin object

    • @dcode-software
      @dcode-software  5 лет назад

      You need to make sure the IFrame URL comes from the same origin as the site that has the IFrame

    • @ozsuakin
      @ozsuakin 4 года назад +1

      @@dcode-software what about working with chrome extension?

    • @YousefTrad
      @YousefTrad 2 года назад

      @@ozsuakin dam, this was my same question...did you figure it out?

  • @rodrigogomescordeiro1616
    @rodrigogomescordeiro1616 Год назад

    MUITO BOM

  • @davidroddini1512
    @davidroddini1512 3 года назад +1

    0:04 “My name is dumb”
    Did he say dumb? Don’t be so hard on yourself dude. I am sure your name is not that bad.