get hostname of page using jquery

In this example, they are using a JavaScript function to get the host name of the page.

https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_loc_hostname


Is there a way to do this exact same thing using jquery ?
LVL 1
maqskywalkerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
Probably since jquery IS javascript.  But that way is probably much quicker since it is less code.
Julian HansenCommented:
As Dave pointed out jQuery is built on JavaScript. Rule of thumb: use jQuery to make your coding easier - don't use it to replace all JavaScript code - jQuery is going to make your coding easier than JavaScript in some respects but JavaScript should always be the first goto - it is more efficient for one thing.

Let's break the code down
function myFunction() {
    var x = location.hostname;
    document.getElementById("demo").innerHTML= x;
}

Open in new window


Firstly we could maybe put a jQuery event handler on the click
$(function() {
  $('button').click(function(e) {
    e.preventDefault();
    var x = location.hostname;
    $('#demo").html(x);
  });
});

Open in new window


But what have we really gained here?
One line of JS
document.getElementById("demo").innerHTML= x;

Open in new window

Becomes one line of jQuery
$('#demo").html(x);

Open in new window

But in the jQuery example we are now having to run code to first wrap the element in a jQuery Object. jQuery is going to call document.getElementById on the element anyway and then run a bunch more lines of JavaScript - that achieves us what?

My rule of thumb - use JavaScript until it becomes a pain and then use jQuery.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
maqskywalkerAuthor Commented:
thanks.
Julian HansenCommented:
You are welcome.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.