Its posible to pass some file path into jquery or javascript function?

I want to pass some files path into a JavaScript or Jquery function, so this can display those files (HTML files) into a div when clicking an a tag.

its possible?

something like this:

  <script>
    $(document).ready(function loadcontent(pathfile){
      $('#content').load("pathfile");
    });
  </script>

The div:

<div id="content">
  HTML FILE HERE
</div>

Upvotes: 0

Views: 587

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337626

It depends on the path. If it's a local path (eg. file://C:/foo.txt) then no, you cannot do this for security reasons.

If it's a relative path to a public resource held on your own server (eg. /folder/file.html) then yes, this is possible. In your case you would just need to fix your syntax to create a valid function and provide the argument to load():

jQuery($ => {
  loadContent('/path-to-your/file.html');
});

function loadContent(path) {
  $('#content').load(path);
}

Upvotes: 1

Related Questions