0

This a basic question. But I couldn't find any duplicates on SO.

This is a script I intend to use in my project on different pages. The purpose is to override the default ID shown in a span element to the order number from the URL parameter session_order. This doesn't affect anything and only enhances the UX for my project.

scripts.js (loaded in the header):

function get_url_parameter(url, parameter) {
    var url = new URL(url);
    return url.searchParams.get(parameter);
}

And in my HTML template, I call the function this way,

<div onload="this.innerHTML = get_url_parameter(window.location.href, 'session_order');">24</div>

Also tried this,

<div><script type="text/javascript">document.write(get_url_parameter(window.location.href, 'session_order'));</script></div>

When the page is rendered, nothing changes. No errors or warnings in the console either for the first case.

For the second case, console logged an error Uncaught ReferenceError: get_url_parameter is not defined. But the script.js loads before the element.

Normally, I'd do this on the server-side with Flask, but I am trying out JavaScript (I am new to JavaScript) as it's merely a UX enhancement.

What am I missing?

Claudia
  • 163
  • 1
  • 11
  • span elements don't load external content, so there is no load event for them – Quentin Nov 13 '19 at 11:47
  • In your second example, you aren't doing anything with the return value – Quentin Nov 13 '19 at 11:48
  • Can you open the question? I have updated it. – Claudia Nov 13 '19 at 12:07
  • @quentin, Can anyone please re-open the question? I can't post a new question for another 90 minutes. – Claudia Nov 13 '19 at 12:45
  • "But the script.js loads before the element" — Either it doesn't, or it has errors in it. Either way, there's not enough information in the question to identify why so it would be closed anyway. – Quentin Nov 13 '19 at 12:57
  • Can you give me an idea on 'not enough information' part. I did edit the question and update it with more information. I am not sure what more information is needed. Perhaps opening it would help others identify the issue with the code. – Claudia Nov 13 '19 at 13:01

0 Answers0