0

Currently, my approach (thanks to so user help) is to load a offsite html into the current page via:

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>   
<script type="text/javascript">
$(document).ready(function(){
    $("#output").load("https://homepageforme.com/pathtofile/Search.html");
});
</script>
    <div class="formClass">
        <div id="output">
        </div>

The only gripe I have here, is that this requires me to specifiy a full "url".

F.e. In iframes I could simply state "Search.html" (similar to a href), but here this won't work.

If there is no other workaround, I'd be ok with "taking the current window url" (window location) (sans the final file of the current page) => https://homepageforme.com/pathtofile/

then adding the fileanme "Search.html"

getting "https://homepageforme.com/pathtofile/Search.html"

that way and being able to feed it to the JS like this.

The reason being, that if I want to migrate the website, I'm forced to edit the paths manually.

Abdennour TOUMI
  • 87,526
  • 38
  • 249
  • 254
Rye
  • 49
  • 5
  • You should NOT update your question with someone answer! You must keep the question as it's , and appreciate someone's answer below. You will be rated down because of this behavior – Abdennour TOUMI Dec 08 '21 at 00:47
  • Is there a way for me to show them the "implementation" otherwise ? I need to know if the way I'd implement it is correct. (I wanted to remove it as soon as I got a reply...) – Rye Dec 08 '21 at 00:49
  • You already described your implementation, and you described your issue – Abdennour TOUMI Dec 08 '21 at 00:50
  • Ok, I'll see if I can get it to work. The code looks clean from what I can see. Very good. (There is no other alternative to this approach eitherhow, as ajax only allows for https anyways) - Kinda amusing if you think about it. – Rye Dec 08 '21 at 00:52
  • Final edit: Here is another approach for those who care: https://stackoverflow.com/questions/17636528/how-do-i-load-an-html-page-in-a-div-using-javascript#comment25679730_17636528 ( but it shares some iframe problems, so the idea in this question here is probably preferred for many usecases) – Rye Dec 08 '21 at 01:11

1 Answers1

1

So you want the URL without the last part (/*.html), then, it's :

window.location.href.match(/(.*)[\/\\]/)[1]

Then, it becomes:

var baseUrl = window.location.href.match(/(.*)[\/\\]/)[1];
$("#output").load(baseUrl + "/Search.html");

NOTE - If you just want to reuse the origin (http://example.com), then use window.location.origin

Abdennour TOUMI
  • 87,526
  • 38
  • 249
  • 254
  • I'd need that without the "/pathtofile/. The entire section should be the variable part if possible. Basically I'd require it to be [everything before.com/pathtofile/] + [Search.html]. But good first attempt ! – Rye Dec 08 '21 at 00:39
  • I updated the answer. Check now @Rye – Abdennour TOUMI Dec 08 '21 at 00:41
  • As I have yet to check how the full code would look (adding an interpretation of your answer to my code seems not to be accepted on SO) I'll accept it and hope I can get it to work. Thanks :) – Rye Dec 08 '21 at 00:51