It's too late...
but I use this method, and I just add it here if someone needs it in the future:
I had this problem when trying to use the same load statement from pages that exist in different URLs (different parts of the site).
you can use location js variable
location returns the current path
e.g. wwww.blog.mysite.com/posts/postn25/comment64
location.origin returns the domain and host (so the root of the site)
for the previous URL, it will be
wwww.blog.mysite.com/posts/postn25/comment64
so when you do
$('#my_tag').load(`${location.origin}/mypath/mypage`)
it will always look for /mypath/mypage , starting from the root directory, so even if the domain changes, it will still work
PS: (unrelated) Today I found out that you can use> * to load everything that is inside a tag in another, for example:
$('#my_tag').load(`${location.origin}/mypath/mypage #my_tag > *`)
will load all the HTML from #my_tag, which exists in /mypath/mypage , into #my_tag on the current page
source share