Reacting differently to links leading to the same page with vanilla JS - javascript

I have two html pages, linking to each other with tags. However, the behavior of the opened page needs to be different, based on which link is clicked to get there.
Example: One of the links is in a drop down header menu, and the new window should start with this menu open and then retract it.
The other link is in a blurb on the front page, and the opened page should therefore not start with the header dropdown opened and then retract it.
Is there a way to modify behaviour of the opened page, based on which link is used to get there?
Cheers.

Pass data to another page as href value. Grab this data using PHP and change the behaviour of new page depending upon different values of this data.
Some reference : Passing values to another php page using anchor tag

Related

Two active links at the same time on wordpress

I have this "problem" with my website that is, when i am at the Home Page i have another link of my menu that is not a page, who stays active because i used an anchor on that link.
Basicaly i added the https://dama.ci/notre-histoire url to that link so when i'm on another page of my website i can actually click on the link and go back to the home page where the anchor is attached and this is what causes the problem.
So my question is, since that link on the menu isn't a page what could i actually do to have the link activated only when it's clicked ?
Is there another way to achieve what i'm trying to have on the website ?
If you want to check the site here is the URL
You can try with another way, you should remove current-menu-item class from that li onload using js i have added sample js code for you
$(document).ready(function() {
$(".menu-item-538").removeClass("current-menu-item current_page_item");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Change your “Services” link URL from:
https://dama.ci/#inspiration
to simply:
/#inspiration
i.e. remember to add your domain’s trailing slash when you use the full or root-relative URL in your links.
When you say two menu items get highlighted at the same time, you mean “inspiration” and “Home”? If yes, you’ll need to make the “Home” link point to the top of the homepage (e.g. with a URL like /#inspiration). Otherwise, it gets highlighted by your theme as the current page.

Determine which link (on same page) was used?

I have a page with two links to an introduction/registration page. We want to show different content based on which link the user uses. They appear in different contexts on the same page, and we would like to tailor the message on the registration page to that context. For SEO purposes I was told not to change the URL.
I'm using rails. Since the incoming user will have the same referrer regardless of which link was clicked, what is the best way to know which link was used to get to my page? Is it possible in a practical way without altering the URL?
I would change the href on click like described in How to change href of <a> tag on button click through javascript
But I would not change the whole url. I would only append a parameter. This should not influence SEO.

Page layout changes slightly, no style changes

I am redesigning my personal / development website, and trying to make it as user friendly as possible.
I wanted to load new pages with AJAX and slide them in, and that's what's currently happening.
Everything works in all current browsers (except IE, but I'll work on that in the future).
However, there's a strange spacing jump in my navigation menu on the first (and only first) page change. After the page changes once, the navigation spacing stays the same.
On page load
After a link is clicked
When a link is clicked, the navigation section from the new page is set to replace the current navigation, because each link has a data-offset attribute that's calculated by the server backend so that the javascript knows which direction and how far to slide the elements when they are clicked.
The way I'm currently doing it, it's either [the content width] or -[the content width].
However, the structure of the menu never changes, and the stylesheet is completely static. I have inspected all of these elements in Firebug, and nothing changes across page loads.
I linked the site, http://next.randolphwebdevelopment.com above, but I'll copy some of the pertinent Javascript here to give an idea of what's happening.
Pulls the entire page down that the link would redirect to (unless specified, all future code blocks are also inside this callback):
$.get( loc, function( data ){
//delete the doctype declaration
data = data.split("\n").slice(1).join("\n");
next_page = $(data.replace(/(\r\n|\n|\r)/gm,"").replace(/>\s*</gm,'><'));
Replaces the navigation with the new navigation from the target page (all that really changes is the data-offset attribute on each link):
//insert the new navigation
$('#header-content nav').html(next_page.find('#header-content nav').html());
$('#header-content nav a').click( navigation_clickHandler );
That's really it. There's a bunch of code to insert the new content and slide it in, as well as changing the page title, setting history states, etc., but this is the only code that touches the navigation menu, and there's no code that touches any stylesheets.
I'm open to any suggestions that fix the problem and/or improve the code design.
At first load nav is got from the server w/o php. I bet you wrote this html manually with intendation and so on.
After any click js recreates nav, but now w/o empty text nodes. Just <a>link1</a><a>link2</a>.
Add spaces or remove them from source html.

Toggle menu issue

I've got a toggle menu, please see http://jsfiddle.net/Wp2em/41/ for code and functions.
On the real site which is using the same code, everytime when you click on h3 (Category 1, 2 & 3 which is an a tag at the moment), it toggles its submenu down a bit, then the page changes to a new h3 linking page, and the submenu collapses together on the new page.
I'm just wondering is there any way I can tell the submenu to be open when its parent page/the new h3 linking page is opened? Please see this bank site which has the side bar effect I'd like my toggle menu to be.
Thank you in advance!
Here is my fiddle
all you will need to do is put the class "currentPage" on the li that you are currently on and the menu should be open after the page loads. I also moved some of your css around so it should move a little smoother now.
** Updated fiddle code. It will now look at your current URL and set the link that matches with it to the currentPage. Also I added that if another menu is open it will close itself if you click on another parent menu
** Updated fiddle code. Ok now if you click on the arrow the menu will expand and not go to the link(like the bank site). Also I changed it where you will have to put the anchor tag in all parent H3s.
This is not too simple. I've had a very similar problem, although I was posting the page back to the same url so I used a hidden field to store a list of the id's of the H3's which were open.
You I think will have to use a cookie to do this as you're navigating straight to the new page. The idea is you create a cookie and set a value on it every time you open an H3 and remove it every time you close it. You can use this plugin to do this. Then when you open the other page, the script reads the H3's which should be open out of the cookie and opens them.
Another route would be to use Ajax to post the open/closed H3 information back to the server which would store it in session data and use it to build the HTML of the new page so the right H3's were open.
If the page loads and the submenu (ul.second_level) is generated (i.e. from php), parse an active css class on the submenu that must be visible.
ul.active {
display: block
}
ul.second_level {
display: none
}
This is in addition to your click function. Do not trigger the click event since it starts the animation (which I presume you don't want).
Update:
It is quite basic stuff, but I do not know how the HTML code for your menu is created. If you are using php and a database (for example) to create the menu, check every submenu item with the page you are on. If the page is one of the pages in the submenu, set the class 'active' on that submenu. The CSS does the rest (displaying this submenu and hide other submenus).
If you have a static page, use javascript to check on which page you are with window.location.href for example. The rest is the same.

Linking to a page and expanding a topic with Javascript

I want to link to a page which contains multiple support topics.
When the user clicks on the link while being on an external site, the topic should be expanded as soon as the user arrives on the support page.
Example:
I link to this page
http://www.nintendo.de/NOE/de_DE/support/nintendo_ds_lite_159_142.html
Topic I want to be opened on arrival
javascript:fadeNAppearDiv('box_3_9277');
(First topic in the FAQs)
It's not clear if you are maintaining the target site (where the div will open) or not. If you don't have access to the code for that page, then there isn't any way to invoke the javascript function on it. If you can modify that page, you can do as #PhiLho suggests and modify the URL you are using to specify the DIV to open and have an onLoad handler that parses the URL and opens the appropriate DIV.
Good idea, but I don't see the question... :-)
The way I saw on some sites, like deviantART, is to use the sharp anchor notation: myURL.com/foo/page.html#TopicToExpand
JS can analyze the current URL and get the anchor part, then do whatever you need to do to highlight/jump to the right place.

Categories

Resources