Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery relative and absolute path

I need determinate this path in jquery , actually i have one file called functions.js and inside of this one function for load url with jquery

The problem it´s the js load in the index of website and the file really in subfolder

<script src="http://www.domain.com/wp-content/includes/themes/mytheme/js/functions.js"></script>

The js called in the index of website it´s into wp-content/includes/themes/mytheme/js

And the load jquery call to : wp-content/includes/themes/mytheme/index_loader.php

I can put the absolute path to index_loader.php in jquery , but my question it´s if it´s possible no use this and calculate the path into js file

Actually :

$("#test").load("http://www.domain.com/wp-content/includes/mytheme/index_loader.php");

It´s possible this or calculate inside jquery file ? - I try and no works .....

$("#test").load("../index_loader.php");

This it´s my problem really , thank´s regards

like image 376
user2536883 Avatar asked Jul 16 '13 04:07

user2536883


2 Answers

The way JavaScript works it that it loads from the file it was called from and now the file it was written in.

In order to do what you need you need to supply the relative path from the current page you're viewing.

Example:

If current page is http://www.domain.com then you'll need to do:

$("#test").load("wp-content/includes/mytheme/index_loader.php");

If current page is http://www.domain.com/wp-content/index.php then you'll need to do:

$("#test").load("includes/mytheme/index_loader.php");

As a side note CSS is not the same way and CSS the relative path is based on the file it's written in.

like image 101
Steven10172 Avatar answered Oct 22 '22 20:10

Steven10172


this is very very late...
but I'm using this method and I'm just adding it here, in case somebody needs it in the future:
i had this problem when trying to use the same load statement from pages existing in different URLs (different parts of the site)

you can use the location js variable

location returns the current path
for example 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 would 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 works

PS: (unrelated) i found out today that you can use > * to load all of what inside a tag in another, for example:

$('#my_tag').load(`${location.origin}/mypath/mypage #my_tag > *`)

would load all the HTML from #my_tag that exists in /mypath/mypage into #my_tag in the current page

like image 39
Mohamed Benkedadra Avatar answered Oct 22 '22 21:10

Mohamed Benkedadra