Reputation: 21
How to display file.txt content with any text on <title id="mytitle"></title>
tag
$(window).on('load', function() {
$("#mytitle").load("/file.txt");
});
file.txt
My Brother
I want to do this
<title id="mytitle">My Brother is Big</title>
How can I add "is big" text to title tag?
Upvotes: 1
Views: 38
Reputation: 337646
To achieve this you can append()
the extra words in the callback function of load()
which is invoked after the AJAX request has completed and added the content.
$(window).on('load', function() {
let $mytitle = $("#mytitle").load("/file.txt", () => $mytitle.append(' is big'));
});
The only thing to note here is that you're attaching the load()
call to the window.load event, which may mean that the DOM is not fully loaded before you try and update it, depending on how quickly your server responds to the AJAX request. It would be safer to perform the call in document.ready instead:
jQuery($ => {
let $mytitle = $("#mytitle").load("/file.txt", () => $mytitle.append(' is big'));
});
Upvotes: 1