Welcome back!
In the previous lesson we ended talking about js basic functions. Hopefully, I gave enough time to get well known with js structure and methods. Remember, Im here not to talk about the meanings, cause it is all free available on the Internet. My goal - to show everything on the example of one simple project.
We have been imitating communication = not real user. But the visitors of the website need to press buttons for example to call these functions, that's what I call real communication - functions are in a waiting mode.
That's why you need
- Functions edit, add, remove post are for authorized persons (of course), that's why you have to make a new page with its layout by using Moqups, for example.
- The registration page should have log and pass, the data will be collected in the mass again (as the posts). And again validating function to check the equals for the log and pass.
- Moreover, you need a function to check (if it's a registration), if the log is already used, again checking in the mass (our data).
- Of course, buttons! Button to log or button to registrate (different valisating functions + add data function same as add post)
- If it's a user's mode, then a list of filters on the page with posts, a button to apply or just by clicking the value of list to call the appropriate function (get post by filter)
- Then if you feel comfortable working with buttons, you can add more, as an example: buttons for social media, buttons to react
Connecting js and html file
Why not with css? Css is already connected with html, so it's like a chain of connections.
Write in the head part:
<script src="name_of_your_script.js"></script>
You can write this way if only your script file is in the same folder with the html one, in another way you have to write its direction.
What goes next?
Decide which part of your body is for script (Where do you need a script part?). Then, you just as other tags write its name - scrtipt. Now it is like a usual tag you work with.
<script> ... </script>
What for do we need a js part in the body part (on the example of my simple project?)
- Your posts mustn't be collected in html. Why? Imagine: your site contains permanent posts, you have initialized in html. Then the user clicks to get post by filter and what he gets? Same permanent posts, cause they are in html, not in js + this get post. So we have to make them temporary. That's why they are colleted only in js, not in html, so delete the part in the body, where you posts are collected.
- I want to call script functions, for example, in the part with icons (to react) in the html file so that the user clicks on a heart and my functions fill this heart with the red color, so as the result we have: the user liked a post.
JS function to show posts
printProduct(data) {
div_element.innerHTML = "";
data.forEach((product, ind) => {
div_element.innerHTML +=`
<table>
<tr>
<td>
<img src="6.jpg" width="170px" height="170px">
</td>
<td>
<p class="text">${product.destination}</p>
<br />
<ul>
<li>${product.tag}</li>
<li>${product.flug}</li>
<li>${product.createAt}</li>
<li>${product.author}</li>
</td>
</tr>
</table>
`
});
}
As you see it looks like I just moved my html part with showing posts to js. Yes, almost the same, but instead of exact names we have appropriate varibles of our post mass.
Did you notice a mistake?
My img is exact, not a variable, so every time I will try to show my posts, they will all have the same image. That's you task - check out how to collect images.
That was my introduction to real communciation. Try to assign buttons and lists to be clicked by the user. And hide everything that shouldn't be permanent by making same functions as printProduct.
More information you can find on my website.
Good luck with your job!
Top comments (0)