{"id":862,"date":"2020-01-21T06:15:00","date_gmt":"2020-01-21T06:15:00","guid":{"rendered":"http:\/\/shaplakanon.com\/wpsupport\/?p=862"},"modified":"2020-02-10T09:24:08","modified_gmt":"2020-02-10T09:24:08","slug":"how-to-get-current-page-url-with-javascript","status":"publish","type":"post","link":"https:\/\/shaplakanon.com\/wpsupport\/how-to-get-current-page-url-with-javascript\/","title":{"rendered":"How to get current page URL with javascript?"},"content":{"rendered":"\n<p>If you are writing some javascript and need to get current current page url, the following code is useful. This is just give an idea to new javascript learners. If you need help, comments below.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-preformatted\"> var origin   = window.location.origin;   \n\/\/ Returns base URL (https:\/\/example.com)\n var url      = window.location.href;     \n\/\/ Returns full URL (https:\/\/example.com\/path\/example.html) \nvar pathname = window.location.pathname; \n\/\/ Returns path only (\/path\/example.html)<\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p class=\"has-text-color has-background has-text-align-center has-medium-font-size has-very-light-gray-color has-vivid-green-cyan-background-color\"><strong><a href=\"http:\/\/shaplakanon.com\/wpsupport\/site\/a2hosting\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"Get 51% off with A2 hosting (opens in a new tab)\">Get 51% off with A2 hosting<\/a><\/strong><\/p>\n<\/div>\n<\/div>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Demo<\/strong>:<\/h4>\n\n\n\n<script type=\"text\/javascript\">\nvar pathname = window.location.pathname; \nvar url      = window.location.href;     \nvar origin   = window.location.origin;   \n        document.write(origin + '<\/br>');\n        document.write(url + '<\/br>');\n        document.write(pathname + '<\/br>');\n      <\/script>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are writing some javascript and need to get current current page url, the following code is useful. This<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[221],"tags":[],"class_list":["post-862","post","type-post","status-publish","format-standard","hentry","category-javascript"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to get current page URL with javascript? - WordPress Support<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/shaplakanon.com\/wpsupport\/how-to-get-current-page-url-with-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to get current page URL with javascript? - WordPress Support\" \/>\n<meta property=\"og:description\" content=\"If you are writing some javascript and need to get current current page url, the following code is useful. This\" \/>\n<meta property=\"og:url\" content=\"https:\/\/shaplakanon.com\/wpsupport\/how-to-get-current-page-url-with-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"WordPress Support\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/facebook.com\/getwpsupport\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-01-21T06:15:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-02-10T09:24:08+00:00\" \/>\n<meta name=\"author\" content=\"ShaplaKanon\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@ruhul24\" \/>\n<meta name=\"twitter:site\" content=\"@ruhul24\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"ShaplaKanon\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/shaplakanon.com\\\/wpsupport\\\/how-to-get-current-page-url-with-javascript\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/shaplakanon.com\\\/wpsupport\\\/how-to-get-current-page-url-with-javascript\\\/\"},\"author\":{\"name\":\"ShaplaKanon\",\"@id\":\"https:\\\/\\\/shaplakanon.com\\\/wpsupport\\\/#\\\/schema\\\/person\\\/f5f224239283c27426dadc2e4cc9a825\"},\"headline\":\"How to get current page URL with javascript?\",\"datePublished\":\"2020-01-21T06:15:00+00:00\",\"dateModified\":\"2020-02-10T09:24:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/shaplakanon.com\\\/wpsupport\\\/how-to-get-current-page-url-with-javascript\\\/\"},\"wordCount\":49,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/shaplakanon.com\\\/wpsupport\\\/#organization\"},\"articleSection\":[\"javascript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/shaplakanon.com\\\/wpsupport\\\/how-to-get-current-page-url-with-javascript\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/shaplakanon.com\\\/wpsupport\\\/how-to-get-current-page-url-with-javascript\\\/\",\"url\":\"https:\\\/\\\/shaplakanon.com\\\/wpsupport\\\/how-to-get-current-page-url-with-javascript\\\/\",\"name\":\"How to get current page URL with javascript? - WordPress Support\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/shaplakanon.com\\\/wpsupport\\\/#website\"},\"datePublished\":\"2020-01-21T06:15:00+00:00\",\"dateModified\":\"2020-02-10T09:24:08+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/shaplakanon.com\\\/wpsupport\\\/how-to-get-current-page-url-with-javascript\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/shaplakanon.com\\\/wpsupport\\\/how-to-get-current-page-url-with-javascript\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/shaplakanon.com\\\/wpsupport\\\/how-to-get-current-page-url-with-javascript\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/shaplakanon.com\\\/wpsupport\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to get current page URL with javascript?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/shaplakanon.com\\\/wpsupport\\\/#website\",\"url\":\"https:\\\/\\\/shaplakanon.com\\\/wpsupport\\\/\",\"name\":\"WordPress Support\",\"description\":\"Wordpress Repair, migration, speed optimization\",\"publisher\":{\"@id\":\"https:\\\/\\\/shaplakanon.com\\\/wpsupport\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/shaplakanon.com\\\/wpsupport\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/shaplakanon.com\\\/wpsupport\\\/#organization\",\"name\":\"WordPress Support\",\"url\":\"https:\\\/\\\/shaplakanon.com\\\/wpsupport\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/shaplakanon.com\\\/wpsupport\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/shaplakanon.com\\\/wpsupport\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2022\\\/05\\\/cropped-logo-9674824.png\",\"contentUrl\":\"https:\\\/\\\/shaplakanon.com\\\/wpsupport\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2022\\\/05\\\/cropped-logo-9674824.png\",\"width\":274,\"height\":55,\"caption\":\"WordPress Support\"},\"image\":{\"@id\":\"https:\\\/\\\/shaplakanon.com\\\/wpsupport\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/facebook.com\\\/getwpsupport\\\/\",\"https:\\\/\\\/x.com\\\/ruhul24\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/shaplakanon.com\\\/wpsupport\\\/#\\\/schema\\\/person\\\/f5f224239283c27426dadc2e4cc9a825\",\"name\":\"ShaplaKanon\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/ef2c8f93a0558c2047555f674c8746b22988ace93eba9262d198d0a28236ce97?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/ef2c8f93a0558c2047555f674c8746b22988ace93eba9262d198d0a28236ce97?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/ef2c8f93a0558c2047555f674c8746b22988ace93eba9262d198d0a28236ce97?s=96&d=mm&r=g\",\"caption\":\"ShaplaKanon\"},\"sameAs\":[\"https:\\\/\\\/shaplakanon.com\\\/wdc\\\/\"],\"url\":\"https:\\\/\\\/shaplakanon.com\\\/wpsupport\\\/author\\\/sabahat\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to get current page URL with javascript? - WordPress Support","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/shaplakanon.com\/wpsupport\/how-to-get-current-page-url-with-javascript\/","og_locale":"en_US","og_type":"article","og_title":"How to get current page URL with javascript? - WordPress Support","og_description":"If you are writing some javascript and need to get current current page url, the following code is useful. This","og_url":"https:\/\/shaplakanon.com\/wpsupport\/how-to-get-current-page-url-with-javascript\/","og_site_name":"WordPress Support","article_publisher":"https:\/\/facebook.com\/getwpsupport\/","article_published_time":"2020-01-21T06:15:00+00:00","article_modified_time":"2020-02-10T09:24:08+00:00","author":"ShaplaKanon","twitter_card":"summary_large_image","twitter_creator":"@ruhul24","twitter_site":"@ruhul24","twitter_misc":{"Written by":"ShaplaKanon"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/shaplakanon.com\/wpsupport\/how-to-get-current-page-url-with-javascript\/#article","isPartOf":{"@id":"https:\/\/shaplakanon.com\/wpsupport\/how-to-get-current-page-url-with-javascript\/"},"author":{"name":"ShaplaKanon","@id":"https:\/\/shaplakanon.com\/wpsupport\/#\/schema\/person\/f5f224239283c27426dadc2e4cc9a825"},"headline":"How to get current page URL with javascript?","datePublished":"2020-01-21T06:15:00+00:00","dateModified":"2020-02-10T09:24:08+00:00","mainEntityOfPage":{"@id":"https:\/\/shaplakanon.com\/wpsupport\/how-to-get-current-page-url-with-javascript\/"},"wordCount":49,"commentCount":0,"publisher":{"@id":"https:\/\/shaplakanon.com\/wpsupport\/#organization"},"articleSection":["javascript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/shaplakanon.com\/wpsupport\/how-to-get-current-page-url-with-javascript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/shaplakanon.com\/wpsupport\/how-to-get-current-page-url-with-javascript\/","url":"https:\/\/shaplakanon.com\/wpsupport\/how-to-get-current-page-url-with-javascript\/","name":"How to get current page URL with javascript? - WordPress Support","isPartOf":{"@id":"https:\/\/shaplakanon.com\/wpsupport\/#website"},"datePublished":"2020-01-21T06:15:00+00:00","dateModified":"2020-02-10T09:24:08+00:00","breadcrumb":{"@id":"https:\/\/shaplakanon.com\/wpsupport\/how-to-get-current-page-url-with-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/shaplakanon.com\/wpsupport\/how-to-get-current-page-url-with-javascript\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/shaplakanon.com\/wpsupport\/how-to-get-current-page-url-with-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/shaplakanon.com\/wpsupport\/"},{"@type":"ListItem","position":2,"name":"How to get current page URL with javascript?"}]},{"@type":"WebSite","@id":"https:\/\/shaplakanon.com\/wpsupport\/#website","url":"https:\/\/shaplakanon.com\/wpsupport\/","name":"WordPress Support","description":"Wordpress Repair, migration, speed optimization","publisher":{"@id":"https:\/\/shaplakanon.com\/wpsupport\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/shaplakanon.com\/wpsupport\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/shaplakanon.com\/wpsupport\/#organization","name":"WordPress Support","url":"https:\/\/shaplakanon.com\/wpsupport\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/shaplakanon.com\/wpsupport\/#\/schema\/logo\/image\/","url":"https:\/\/shaplakanon.com\/wpsupport\/wp-content\/uploads\/sites\/2\/2022\/05\/cropped-logo-9674824.png","contentUrl":"https:\/\/shaplakanon.com\/wpsupport\/wp-content\/uploads\/sites\/2\/2022\/05\/cropped-logo-9674824.png","width":274,"height":55,"caption":"WordPress Support"},"image":{"@id":"https:\/\/shaplakanon.com\/wpsupport\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/facebook.com\/getwpsupport\/","https:\/\/x.com\/ruhul24"]},{"@type":"Person","@id":"https:\/\/shaplakanon.com\/wpsupport\/#\/schema\/person\/f5f224239283c27426dadc2e4cc9a825","name":"ShaplaKanon","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/ef2c8f93a0558c2047555f674c8746b22988ace93eba9262d198d0a28236ce97?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/ef2c8f93a0558c2047555f674c8746b22988ace93eba9262d198d0a28236ce97?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ef2c8f93a0558c2047555f674c8746b22988ace93eba9262d198d0a28236ce97?s=96&d=mm&r=g","caption":"ShaplaKanon"},"sameAs":["https:\/\/shaplakanon.com\/wdc\/"],"url":"https:\/\/shaplakanon.com\/wpsupport\/author\/sabahat\/"}]}},"_links":{"self":[{"href":"https:\/\/shaplakanon.com\/wpsupport\/wp-json\/wp\/v2\/posts\/862","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shaplakanon.com\/wpsupport\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shaplakanon.com\/wpsupport\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shaplakanon.com\/wpsupport\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shaplakanon.com\/wpsupport\/wp-json\/wp\/v2\/comments?post=862"}],"version-history":[{"count":7,"href":"https:\/\/shaplakanon.com\/wpsupport\/wp-json\/wp\/v2\/posts\/862\/revisions"}],"predecessor-version":[{"id":869,"href":"https:\/\/shaplakanon.com\/wpsupport\/wp-json\/wp\/v2\/posts\/862\/revisions\/869"}],"wp:attachment":[{"href":"https:\/\/shaplakanon.com\/wpsupport\/wp-json\/wp\/v2\/media?parent=862"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shaplakanon.com\/wpsupport\/wp-json\/wp\/v2\/categories?post=862"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shaplakanon.com\/wpsupport\/wp-json\/wp\/v2\/tags?post=862"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}