-2

I have a problem I want to change the Automatic year with javascript
as in php is this possible?

document.write (new Date (). getFullYear ()); this works but want to plug that into var, but then page is completely off just a year this and screen shot [enter image description here][1] [1]: https://i.stack.imgur.com/s8OA8.png

$( document ).ready(function() { console.log( "ready copyright" );

        var copyright = 'Copyright©';
        var static_year = 2014;
        var test = document.write(new Date().getFullYear());
        var company ="Hondenverzorgingleentje";
        var company_url = 'https://www.hondenverzorgingleentje.be';
      
        document.getElementById("copyright").innerHTML = copyright + 
        static_year + company + copyright;


    }
Copyright©'.$static_year.' - ' .$current_year.' ' .$company.' Alle rechten voorbehouden  ' .$designer.''; ?>
PcNerd
  • 1
  • 3
  • It's not clear to me what you're asking. Perhaps you could update the question to include a runnable code snippet which fully demonstrates the problem? – David Oct 27 '20 at 17:58
  • Please review [ask] and [the help center article on how to format code](https://stackoverflow.com/help/formatting). – Heretic Monkey Oct 27 '20 at 17:59
  • Echoing @David, you can create a [MRE](https://stackoverflow.com/help/minimal-reproducible-example) using something like [codepen](https://codepen.io/pen/) – Dylan Landry Oct 27 '20 at 18:00
  • @DylanLandry Or [Stack Snippets](https://meta.stackoverflow.com/q/358992/215552) (icon looks like `<>`) right here on Stack Overflow... – Heretic Monkey Oct 27 '20 at 18:01
  • https://developer.mozilla.org/en-US/docs/Web/API/Document/write – Teemu Oct 27 '20 at 18:03
  • who taught you to us document.write for a variable? `var test = new Date().getFullYear(); console.log(test);` – epascarello Oct 27 '20 at 18:10
  • Welcome! Please edit your code to show code quote/snippet = it spilled... Also, add `jquery` tag and/or any other library you might be using. It's best if you use a snippet here and not codepen/other third-party links. Good luck! – iAmOren Oct 27 '20 at 19:11

2 Answers2

2

You can easily add all strings using template literals instead of using plus to concat strings.

So, try this with the dynamic year:

document.getElementById("demo").innerHTML = `Copyright&copy; ${new Date().getFullYear()} Hondenverzorgingleentje ${designer}`;

You are probably getting blank page because of the Document.write() call. Please see here for the details on why it may clear the page.

If you want to have a clickable company name, define company and company_url variables, and then you can use this code

document.getElementById("demo").innerHTML = `Copyright&copy; ${new Date().getFullYear()} <a href="${company_url}">${company}</a> ${designer}`
Ozgur Sar
  • 2,067
  • 2
  • 11
  • 23
  • okay but could the company have a link – PcNerd Oct 27 '20 at 19:03
  • @PcNerd yes, the company can have a link. Just wrap it inside an A element with the link. – Ozgur Sar Oct 27 '20 at 19:25
  • you can do that from a variable var = 'company'; var company_url = 'https://www.test.be' and mention it in document.getElementById ("demo"). innerHTML = – PcNerd Oct 28 '20 at 05:12
  • Yes, absolutely you can do that. Make sure your company_url variable starts with http:// or https. I edited my answer with the suggested code that includes company name with the link. – Ozgur Sar Oct 28 '20 at 07:52
  • this in a javascript please Copyright©'.$static_year.' - ' .$current_year.' ' .$company.' Alle rechten voorbehouden  ' .$designer.''; ?> – PcNerd Oct 28 '20 at 09:19
  • Check this codepen please: https://codepen.io/ozgursar/pen/qBNpzjo – Ozgur Sar Oct 28 '20 at 13:27
  • Happy to help. If this answer, or any other one solved your issue, please mark it as accepted. – Ozgur Sar Oct 29 '20 at 04:36
  • thank you very much and can you also add span id copyricht and urls so you can style them – PcNerd Oct 29 '20 at 08:36
  • In the codepen link, I added classes to the company and designer links. You can now target them in css by #copyright .company {} and #copyright .designer selectors. – Ozgur Sar Oct 29 '20 at 08:56
  • I have tried but this does not work – PcNerd Oct 29 '20 at 09:05
  • Can you please elaborate? What didn't work? I updated the codepen example once more to include custom styling with the casses. Please take a look: https://codepen.io/ozgursar/pen/qBNpzjo – Ozgur Sar Oct 29 '20 at 10:45
  • ok thanks a lot, i'm also trying the same for the days when you case is open and closed – PcNerd Nov 02 '20 at 18:19
0

I think you want to display the current year on your webpage.

If I were you I would create a HTML id/class to access it via js and update the content when the page loads.

If you only need to set the year at one place you can use this:

<p>Copyright 1234-<span id="current-year"></span></p>
<script defer src="script.js"></script>
// script.js
document.getElementById('current-year').innerText = new Date().getFullYear()

If you need to display it at multiple locations you need to use html classes.

<p>Copyright 1234-<span class="current-year"></span></p>
<script defer src="script.js"></script>
// script.js
Array.from(document.getElementsByClassName('current-year'))
  .forEach(el => el.innerText = (new Date()).getFullYear())

Notes: the defer attr. in the script tag will block the execution of the javascript until your html dom is loaded.

getElementsByClassName returns an array-like object and not an array, so I converted it into an array and used array#forEach. I could have used a regular for loop on the array-like object but I prefer this oneliner.

tomitheninja
  • 497
  • 2
  • 13