0

After a few days of searching and trial and error i'm still unable to get this to work at all without crashing.

What i'm trying to achieve is changing what text will be displayed in a H3 tag in an .ejs file.

The reason for this is because the system we're building is using partial of another file so rather than creating more files only this tag has to change it's text.

Section in Question:

<%if (window.location.href === '/newWizard') { %>
  <h3><strong>Step of a Wizard </strong> - Some Text</h3>
<% }else { %> 
  <h3>Same Text</h3> 
<% } %>

this file is referenced by <%- partial %> and that file is referenced one step back by <%- include %>.

We're building this software off a Node.js and Kendo Grid design.

Any help with this would be greatly appreciated, i'm going to continue research and work on it, will update this if i manage to have it to work properly.

Lucas
  • 1,337
  • 7
  • 15

1 Answers1

0

window.location.href, if I'm not mistaken, will give you the full URL. You probably want to check window.location.pathname in order to do the check in the way you've described.

You could also do some pattern matching on the href or pathname, depending on your needs.

Update

Since it's .ejs, it's getting executed on the server, so there is no window object.

Have a script run on the client side. Create the <h3> in the HTML and give it some id like <h3 id="title-heading"></h3> and then add a script doing something like

if (window.location.pathname === '/newWizard') {
    document.getElementById('title-heading').innerHTML = '<strong>Step of a Wizard </strong> - Some Text';
}
else {
    document.getElementById('title-heading').innerHTML = 'Same Text';
}
Lucas
  • 1,337
  • 7
  • 15
  • Checked my log after trying that and it's still saying 'window' not defined... So the problem may be i don't have access to those calls in this file. – Jonah Compton Dec 15 '15 at 20:01
  • 1
    I'm not too familiar with it, but it could be that since it's .ejs, it's getting executed on the server, so there is no window. You might want to do that JavaScript client-side. I'll update with a snippet. – Lucas Dec 15 '15 at 20:05
  • 1
    Are you rendering this in node and sending it to the client? As @Lucas said, there is no `window` object in node. You'd have to take the origin of `req` and send it to your controller. – Daniel Lizik Dec 15 '15 at 20:06
  • The above has solved my crash issue but for some reason i'm not getting "Cannot set property 'innerHTML' of null. Does this mean i'm trying to pass something with no value to this because it doesn't start in /newWizard or should i not use innerHTML? – Jonah Compton Dec 16 '15 at 19:08
  • It could be because your JavaScript executing before your HTML is loaded. Then it wouldn't find the element you're referring to. (http://stackoverflow.com/questions/807878/javascript-that-executes-after-page-load, https://css-tricks.com/async-attribute-scripts-bottom/) – Lucas Dec 16 '15 at 21:53