pushState and popstate

Поділитися
Вставка
  • Опубліковано 28 вер 2024
  • I was so confused when I first learned this API; I hope you don't get confused!!! history.pushState and window.onpopstate are the two main things used in this tutorial to affect what the browser's back and forward buttons do.
    Get the code from GitHub: github.com/Gee...
    Live demo: geeklaunch.git...
    geeklaunch.net/
    Support GeekLaunch on Patreon: / geeklaunch
    Follow GeekLaunch on Twitter: / geek_launch
    GeekLaunch produces educational videos covering such topics as Linux, web development (including HTML5, CSS 3, JavaScript, and PHP), tips for power users, among many others. Subscribe: New video every Wednesday!
    Not a geek? Start today!

КОМЕНТАРІ • 64

  • @tabgimbarr
    @tabgimbarr 3 роки тому +1

    Dynamic changing title is actualy supported by any browsers, it just needs right way to code it. The code is document.title = 'test'

  • @soy-dave
    @soy-dave 5 років тому +4

    Great explanation! Didn't know this API existed in HTML5.

  • @ruzzlgreen3436
    @ruzzlgreen3436 3 роки тому +1

    I love how soothing your voice is

  • @pedrocarlosdeoliveiradossa656
    @pedrocarlosdeoliveiradossa656 Рік тому +1

    11:28 Thanks for your time, excelent tutorial.

  • @artemnakhodkin9548
    @artemnakhodkin9548 6 років тому +10

    Cool! Pretty well explained!
    But it would be worth to mention, that history object also has a property "state", that handles current state. For example, if your page loaded initially, there would be no "popstate" event fired.
    To make the conjunction between application state and history API state, you'll have to read history.state property to make a proper initialization.

    • @GeekLaunch
      @GeekLaunch  6 років тому +1

      Hey, thanks for the extra insight, it's really appreciated.

  • @namng5422
    @namng5422 3 роки тому +1

    Best tutorial , please more webAPIs !!!

  • @qzbnyv
    @qzbnyv 4 роки тому +1

    Your voice reminds me of Jon Lovett's (I listen to too many podcasts). Also great video, very helpful example to learn this API's basics. Thanks!

    • @qzbnyv
      @qzbnyv Рік тому +1

      So I’m back here watching this video again several years later.
      Have essentially only been using hashchange since back then, but figured it’s time to properly understand popstate/pushState and implement it in PureScript.
      (I’m listening to basically zero podcasts these days, and definitely no political stuff anymore, but am addicted to silly LitRPG audiobooks instead)
      (If I’m back here in another few years watching the video again, I probably won’t comment as it’d give away a lack of progress 😅)

  • @cr4zyd4n666
    @cr4zyd4n666 6 років тому +1

    Hey this video was extremely helpful it took me ages to get it but ive downloaded the code and played around with it and now i get it! I think i've solved the problem on my site now thank you

    • @GeekLaunch
      @GeekLaunch  6 років тому +1

      That's awesome, glad to hear you figured it out!

  • @ronaldc8634
    @ronaldc8634 5 років тому

    The code in the video it will break on refresh because the browser will request a nonexistent page.
    How do I make it an existing page?

  • @RaldinCasidar
    @RaldinCasidar 4 роки тому

    I know a library that will let all major browser support the entire history api .
    Its called history.js on github . check it !

  • @GuitaristZep
    @GuitaristZep 2 роки тому

    how to know if user goes forward or back using popstate?

  • @mohanmongia3704
    @mohanmongia3704 3 роки тому

    Just a question. When we use history.pushState() , doesn't it pushes to a stack rather than an array as mentioned by you?

    • @aiamfree
      @aiamfree Рік тому

      I think you're correct because in popstate you just call it as e.state without indexing...

  • @drusttaib607
    @drusttaib607 4 роки тому

    hey man thanks for the video, I have some questions regarding ajax. 1- if you check youtube, they are using ajax to load parts of the html, but what is surprising is, you can also navigate different parts with url link, (i mean, if they change some parts of the html, it means they have only one web page) . How to change different parts of the html and make it accessible with link also? 2- the forward and backward event you mentioned. how can we save an entire div with the elements inside it, and how can we save the entire html page?
    please tell me if you didn't get any of these

    • @GeekLaunch
      @GeekLaunch  4 роки тому

      For your first question, the answer would be that the changes UA-cam performs on the client side when navigation occurs result in the same (or close enough to the same) UI as the server would generate were you to access the new URL directly. Not sure I completely understand the second question.

    • @drusttaib607
      @drusttaib607 4 роки тому

      @@GeekLaunch i have a
      And the only thing that I change with ajax is the elements inside that div. How can I create pushstate for backward and forward with my situation?

  • @Matthew-cp1zh
    @Matthew-cp1zh 4 роки тому +1

    *When he hovered over those boxes.*
    *Anyone else hear "ee er ee er"*

  • @omar99ize
    @omar99ize 5 років тому

    what happens in case someone hit the reload button? Thanks for the video!

    • @GeekLaunch
      @GeekLaunch  5 років тому +1

      Sorry I missed your comment! The API will still work as intended, but the way I wrote the code in the video it will break because then the browser will request a nonexistent page. What I should have done is make a box click change a query parameter instead of the actual path of the URL. :/

  • @JohnmarkAlvarado-ic4vf
    @JohnmarkAlvarado-ic4vf 10 днів тому

    Big Thanks bros

  • @tonl6361
    @tonl6361 3 роки тому

    That code doesn`t work in old IE version.

    • @GeekLaunch
      @GeekLaunch  3 роки тому +2

      Stop using old versions of IE!!

  • @Vietnamcamping89
    @Vietnamcamping89 6 років тому

    why this can't running on my project (On server ) :(

    • @GeekLaunch
      @GeekLaunch  6 років тому

      I don't know, sorry! Hopefully you get it fixed!

  • @kobenade
    @kobenade 4 роки тому

    'Completely pointless' technology that is the basis of single-paged applications :)

  • @play.7242
    @play.7242 Рік тому

    Its like you sade youseless. But the basic is require for individual project

  • @CodeWithMukeshBadgujar
    @CodeWithMukeshBadgujar 4 роки тому +1

    One Problem in your code, i download from your git and run it, now see, when i click any of div and if i refresh the page, then url not works, gives Error : The requested URL /pushstate-and-popstate-master/selected=box-2 was not found on this server.
    Plzz tell me how to solve it...

    • @KnightAlmighty
      @KnightAlmighty 4 роки тому +1

      Because you should use urls of existing pages. Watch a piece after 6:10

  • @aiamfree
    @aiamfree Рік тому

    Building a state tool for a SPA-webapp.. not fun lol...!

  • @sauldownbadman876
    @sauldownbadman876 Рік тому

    What does the e => do? I'm really confused by it

  • @biancotore
    @biancotore 2 місяці тому

    Thanks, very good tutorial!

  • @wirandhikablogs
    @wirandhikablogs 7 років тому +2

    greats, Thanks aLot man!

    • @GeekLaunch
      @GeekLaunch  7 років тому

      +Wira Andhika You're welcome, glad you liked it!

  • @jesusonherra7596
    @jesusonherra7596 Рік тому

    why do you use if else to pass null value? why not just using selectBox(e.state?.id); ? It would eventually pass null where needed

    • @Maxim9575
      @Maxim9575 Рік тому

      VIdeo captured 5 yrs ago, there wasn't optional chain.

  • @adhoccerswings
    @adhoccerswings 5 місяців тому

    Awesome! Btw: I tried this as files (=clicking on the index.html in my folder), and that didn't work -> had to do local hosting (used Five Server). When I didn't do local hosting, my browser lit up like a Christmas tree and yelled at me quite meanly:
    "
    Security Error: Content at file://[my_folder_location_here]selected=box-2.
    Uncaught DOMException: The operation is insecure.
    "
    so that ^^ was when I tried WITHOUT local hosting.
    Side note: I think you can write shorter:
    "let boxes = document.querySelectorAll(".box")"
    This selects all the elements with class "box".

  • @xsomili5501
    @xsomili5501 4 роки тому

    as a new learner who is bad at javascript, i find this easy to follow:) thankyou

  • @omshiv3179
    @omshiv3179 Рік тому

    Thankx for your good explanation sir! It was really helpful for me.

  • @wadiemh
    @wadiemh 3 роки тому

    Thanks dude, that was super helpful

  • @glyakk
    @glyakk 2 роки тому

    Still a great tutorial, also the sassy argument you had with yourself 6:40 was amusing ;)

  • @vishaltaywade8069
    @vishaltaywade8069 2 роки тому

    Nice video 👌 appreciate your efforts 👍

  • @sobirmalilov8838
    @sobirmalilov8838 3 роки тому

    Thanks bro your tutorial is useful for me

  • @Shuvooa
    @Shuvooa 4 роки тому

    Very good explanation

  • @chickendev5759
    @chickendev5759 4 роки тому

    thank you so muchhhhhhhhh

  • @trfaruk44
    @trfaruk44 5 років тому +1

    Thank you :)

  • @Couchwurst
    @Couchwurst 4 роки тому

    Nice! A concise and really well rounded video! Thank you.

  • @suhaib404
    @suhaib404 6 років тому

    That was on point! Exactly what I was looking for. Subscribed!!

    • @GeekLaunch
      @GeekLaunch  6 років тому +1

      Thanks for subscribing! I'm currently on an extended break (school + work) but I'll be sure to produce more content in the future!

  • @kayoscreed
    @kayoscreed 6 років тому

    It's great, I can use it for sure. Also very helpfull with stateless webapps to store all Parameters in and reload.

    • @GeekLaunch
      @GeekLaunch  6 років тому

      +Kayoscreed Thanks for watching!

  • @puneettripathi8418
    @puneettripathi8418 5 років тому

    which text editing software you are using?

    • @GeekLaunch
      @GeekLaunch  5 років тому

      Sorry I missed your comment - I'm using Visual Studio Code in this video.

  • @ukwerna
    @ukwerna 5 років тому

    Cheers!

  • @Shaambhavee9
    @Shaambhavee9 6 років тому

    Thanks for sharing this!

  • @LonIlesanmi
    @LonIlesanmi 7 років тому

    This is cool man

    • @GeekLaunch
      @GeekLaunch  7 років тому

      Thanks, I'm glad you think so!

  • @nafaa-news
    @nafaa-news 4 роки тому

    You know what made me happier in this video? it's your last comment, "I had a heck of a time learning this API". So, I'm not the only one got trouble with this API.
    Thank you for this clear explanation.