Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //bookmark index.html
- <!DOCTYPE html>
- <html lang="en" dir="ltr">
- <head>
- <meta charset="utf-8">
- <title>Bookmark Saver</title>
- <link rel="stylesheet" href="css/bootstrap.min.css">
- <link rel="stylesheet" href="css/style.css">
- </head>
- <body>
- <div class="hdr">
- <h3 align="center" class="title">BOOKMARK SAVER</h3>
- </div>
- <div class="user-form">
- <form id="myForm">
- <div class="form-group">
- <label for="userName">Username :</label>
- <input type="text" class="form-control" id="userName" placeholder="Enter your username">
- </div>
- </div>
- <div class="form">
- <div class="form-group">
- <label for="siteName">Site Name :</label>
- <input type="text" class="form-control" id="siteName" placeholder="Enter your site name">
- </div>
- <div class="form-group">
- <label for="siteURL">Site URL :</label>
- <input type="text" class="form-control" id="siteURL" placeholder="Enter your site URL">
- </div>
- <button type="submit" class="btn btn-primary">SAVE</button>
- <button type="reset" class="btn btn-primary">RESET</button>
- </form>
- </div>
- <button id="view" class="btn btn-primary" onclick="fetchBookmark(document.getElementById('userName').value)">VIEW</button>
- <div class="form">
- <table class="table">
- <h4 id="userTitle"></h4>
- <thead class="thead-dark">
- <th scope="col">Site Name</th>
- <th scope="col">Site URL</th>
- <th scope="col"></th>
- <th scope="col"></th>
- </thead>
- <tbody id="myView"></tbody>
- </table>
- </div>
- <script type="text/javascript" src="js/jquery-3.4.1.min.js"></script>
- <script type="text/javascript" src="js/main.js"></script>
- </body>
- </html>
- //style.css
- .title{
- background-color: lightgrey;
- padding: 10px;
- }
- .user-form{
- margin-left: auto;
- margin-right: auto;
- margin-top: 5em;
- background-color: lightgrey;
- padding: 20px;
- width: 70%;
- border-radius: 10px;
- }
- .form{
- margin-left: auto;
- margin-right: auto;
- margin-top: 2em;
- background-color: lightgrey;
- padding: 20px;
- width: 70%;
- border-radius: 10px;
- }
- .form-control{
- width: 50%;
- }
- #view{
- margin-left: 50%;
- margin-top: 20px;
- }
- //main.js
- document.getElementById('myForm').addEventListener('submit', saveBookmark);
- function saveBookmark(e){
- window.userName= document.getElementById('userName').value;
- var siteName = document.getElementById('siteName').value;
- var siteURL = document.getElementById('siteURL').value;
- var bookmark={
- website: siteName,
- url : siteURL
- }
- if (!userName || !siteName || !siteURL) {
- alert('Please fill in all field');
- }
- else if (localStorage.getItem(userName)==null) {
- users = [];
- users.push(bookmark);
- localStorage.setItem(userName, JSON.stringify(users));
- }else {
- var addSite = JSON.parse(localStorage.getItem(userName));
- for (var i = 0; i < addSite.length; i++) {
- if (addSite[i].url == siteURL) {
- alert('URL already exist!!');
- return false;
- }
- }
- addSite.push(bookmark);
- localStorage.setItem(userName, JSON.stringify(addSite));
- }
- e.preventDefault();
- fetchBookmark(userName);
- document.getElementById('myForm').reset();
- }
- function fetchBookmark(userName){
- var bookmarks = JSON.parse(localStorage.getItem(userName));
- var result = document.getElementById('myView');
- var userTitle = document.getElementById('userTitle');
- userTitle.innerHTML = "<h4>Bookmark For : "+userName+"</h4>";
- result.innerHTML = "";
- if (bookmarks.length == 0) {
- result.innerHTML += "<h4> No Bookmark Founded!</h4>";
- }
- else {
- for (var i = 0; i < bookmarks.length; i++) {
- var name = bookmarks[i].website;
- var url = bookmarks[i].url;
- result.innerHTML += "<td>"+name+"</td>"+
- "<td>"+url+"</td>"+
- '<td>'+'<a class="btn btn-warning"+target="_blank" href="'+ url +'">VISIT</a>'+'</td>'+
- '<td>'+'<a onclick="deleteBookmark(\''+userName+'\',\''+url+'\')" class="btn btn-danger"+ href="#">DELETE</a>'+'</td>'
- +'<br>';
- }
- }
- }
- function deleteBookmark(userName, url){
- var bookmark = JSON.parse(localStorage.getItem(userName));
- for (var i = 0; i < bookmark.length; i++) {
- if (bookmark[i].url == url) {
- bookmark.splice(i,1);
- }
- }
- localStorage.setItem(userName, JSON.stringify(bookmark));
- fetchBookmark(userName);
- }
Advertisement
Add Comment
Please, Sign In to add comment