How to add a jump event using Javascript on Wordpress - javascript

So in short, I have this page I created on a wordpress site. The issue, the content on the page, divided in different rows, is quite large and I wanted to enable on the beguinning of the page a jump to section link (and jump back to top link) for each section (in this case, row).
I'm in doubt, though, on what is the best way to do this on wordpress and wether I shoudl add an even type like scroll to or jump to. Also, I want to create a function that does this to multiple sections... in order to have a neat and simple code, is there any function(s) options you could recomment me to search and try to use?
thank you =)

The easiest way you can create a link to "jump to" a section of the page is with anchor links. This page has an example:
https://www.rapidtables.com/web/html/link/html-anchor-link.html
Notice that the browser URL gets updated when you click the anchor link, this is how you can share links to specific sections of your page.
How to create anchor links in Wordpress (Visual Edit Mode):
Begin by adding the anchor tag. This will go on the section header that you want to jump to (ie, the destination of the link). In Wordpress Visual Edit mode, on the right-hand side of the screen you should see an Advanced section. In the "HTML Anchor" field, add your tag. Use dashes (-) instead of spaces. Here's an example: my-section
Now create the link to the tag. Highlight the text that will link to the anchor tag. Click the "link" button in the toolbar and add the anchor tag with a pound symbol (#) in front of it. Example: #my-section
Save and publish. Now when you click the link you created in step 2, it should jump you to the section you tagged in Step 1.
For more detailed instructions, please see the Wordpress.com guide:
https://wordpress.com/support/splitting-content/page-jumps/

Related

Reference to part of the site using existing JavaScript animation

I am trying to design a website. I decided to work with this template from templatemo.
I am currently trying to add a reference to a specific part of the website (e.g. Contact) in the "normal text area". I tried to just add a href in the text, like this:
Contact me via the <a href="#contact_form" >Contact Form</a>
This does not work properly. If I click on the reference, the whole layout is somehow messed up. I assume this is because the click on the link does not trigger the javascript function for the "changing the slide" properly.
My question is now:
How can I trigger the javascript functions, for "changing the slide" to a specific part of the page in regular text areas?
This animation is already implemented and works properly (e.g. it is triggered when clicking on the buttons in the navigation bar). However, I have no clue how to use the same animation for jumping to a part of the page outside of the navigation bar.
I hope you understand my problem.
Thank you in advance for your support!
Ideally, you would have to modify the page contents in the respective location in the HTML file. The way this file is constructed is that you have a ul list with li elements that indicate each page, and each li element is given a numerical value that represents its position in the list (e.g. 1, 2, 3, ...). Make sure to edit and add any new pages in this part of the markup. If you want to adjust or trigger any page transitions, edit the data-no value in the <a> tag in #tmNavbar to the position of the page in the list.
If you want to trigger the transition from elsewhere in the website, I would recommend just clicking on the respective nav-item using Javascript. For example,
$('.nav-item').get(i).click() /* i is the data-no value` */

HTML Semantics - Button acting as an anchor

I'm mainly interested in the a11y aspects
So as you might be aware, sometimes you might wish to have a button that acts as an anchor.
These are 4 ways (I can think of) of approaching this issue:
1. Button inside an anchor element
<button>Button</button>
2. Anchor inside button element
<button>Button</button>
<!-- Also should it be a href or button onclick (?) -->
3. Anchor styled as a button (without a button element)
<a class="buttonLike" href="//redirection">Button</a>
4. Button acting as a redirection (without an anchor element):
const button = document.getElementById('button')
button.addEventListener('click', () => {
window.location.href = "//redirection"
})
<button id="button">Button</button>
I've tried to find an answer to this myself. Closest I could find was this excerpt:
According to the MDN anchor spec, which states the following:
Anchor elements are often abused as fake buttons by setting their href to # or javascript:void(0) to prevent the page from refreshing, then listening for their click events .
These bogus href values cause unexpected behavior when copying/dragging links, opening links in a new tab/window, bookmarking, or when JavaScript is loading, errors, or is disabled. They also convey incorrect semantics to assistive technologies, like screen readers.
Use a <button> instead. In general, you should only use a hyperlink for navigation to a real URL.
Unfortunately this doesn't help me too much. Basically all it states is you should not use the third approach (anchor styled as a button) if you don't mean to provide a real link to it, which is not what this question is about.
Is there any official WCAG on this subject matter that I was unable to find or?
Option 1 is not valid HTML.
Option 2 is not valid HTML.
Option 3 is the correct choice here.
Option 4 is semantically incorrect.
Semantics are one of if not the most important aspects of accessibility.
There are two things at play which dictate option 3.
The first is that an anchor should be used only to jump to sections and to navigate between pages.
The second is that a button should perform actions on the same page.
Now if you want to style a call to action link to look like a button that is fine but make sure you use the correct semantics, but make sure that CTA leads to a new page or it isn't semantically correct.
And although it is swearing on StackOverflow to mention SEO, a hyperlink rather than a JavaScript redirection will be far better.
The first and second rules of ARIA say:
1st rule : If you can use a native HTML element [...] then do so
2nd rule : Do not change native semantics, unless you really have to.
Interactive elements like a and button can't contain other interactive elements:
The a element may be wrapped around entire paragraphs, lists, tables, and so forth, even entire sections, so long as there is no interactive content within (e.g. buttons or other links).
So, as what you want to do is linking to a page, your third solution is obviously the only one correct.
I think you might have confused the "bogus" stagement which refers to your 4th example.
From my little experience with Accessibility and semantics there is no "one size fits all". It really depends on your priorities and the user experience you are aiming for.
A <button> gets all the accessibility goodies from the browser automatically: Being selected or pressed using the tab or spacebar/enter keys.
A <a> element is a link, links are meant to be used as links or anchors within a page.
Anchors are not as important in comparison to a button within a page. From a user experience point of view; a button is used by people to interact with a UI, either to confirm or make the UI do something. Pressing a button provides a different feedback compared to a link. Anchor links on the other hand help a user with finding content within a page.
Again, it really depends on what you are trying to do:
Is this a terms page or an article? Then list your anchor links without any button-like styling
Does this a link that has to look as a button so users find it easier to spot or interact? Then style it as a button without it being actually a <button>.

Remove appended anchor/#link from url after use

Over the past month I've been working on a new website which is based on a WordPress theme. The site is a pseudo one-page design, where the primary page serves as the main page for information. However, it also has a gallery where you can enter several individual portfolio projects - which effectively takes you to another page (and therefore the use of "pseudo"). So that's the design and layout of the site...
On the main page of the site I added anchor links to the various sections for ease of use, which will scroll/take the user to the desired section of choice, by simply clicking on the navigation menu. The links all work fine, but a problem arises when you've entered and returned from a portfolio project.
The problem:
After having clicked on an anchor link, a hashtag is then added to the URL "www.example.com/#anchor". If the user enters a different page and then returns the hashtag/anchor is then reactivated and scrolls to the linked section. This is a real pain and anything but user-friendly, since it is taking the user away from where it was.
I've been trying as many Javascript/JQuery codes I can find, many from this very site, but none has managed to solve the problem... I need to find a way to effectively remove the hashtag after use so the URL is left with only "www.example.com" again.
EDIT: Forgot to add that I also need to be able to click on the navigation menu whilst in a portfolio project and having the menu call to open the main page and take the user to the section of choice.
Does anyone have a solution? I am all out of ideas here.
Please also tell me where to add the piece of code if you are so kind to help.
Thank you!

Scroll to position after reload

I'm not that into Javascript but I think this could be achieved easily.
I've got a link on my page that sets a parameter (with PHP). That parameter is used to get how much posts to show. This works like a charm.
But now I want to scroll to the postition where the user clicked the link after the page reloads with the parameters. So the users doesn't have to scroll through the posts he already saw.
So here are the steps:
User clicks link to load more posts
Site gets reloaded with parameter (e.g. domain.com/?numberofposts=10)
Now the Site should scroll to the position the users was when he clicked the link
I was trying to achieve that with scrollTo etc but I cant get it working. My thaughts were to pass the scrollposition as an other parameter maybe?
Do someone know how to solve this? Thank you :)
By including an anchor tag within a post or page, you can place links in the body of your post which when clicked allow the reader to jump to another location on the page.
The anchor tag will consist of two HTML elements. First, you'll want to create the link.
If you are linking to a spot on the same page, the format of the link will be similar to:
Link Text
For example, if the text is "Read more about raptors!" then your HTML should look like this:
Read more about raptors!
The above anchor link only works when you are jumping to a specified spot on the same webpage. If you want a link to jump a specific location on a different page, you'll need to replace #anchor with the full URL for the page, similar to:
Link Text
The second part of an anchor tag is the actual anchor. The anchor should be placed at the beginning of the line where you want to start reading after you jump similar to:
<a name="anchor"></a>
Following our previous example, the anchor code will be:
<a name="raptors"></a>
Full tutorial here
You need to know where you have to scroll to.
So yes, you do need to store the scrollposition somewhere (or pass it as a parameter).

Is it possible to hide link address on hover?

I have set up a chart with a lot of links and it really bugs me when it shows where the link goes in the bottom left hand side of the browser whenever you hover on a link, like so:
Is it possible to remove this? any method will do as long as I can hide/remove it (HTML, CSS, JS etc..)
The status bar highlighting happens only when you use an <a> element with a set href.
If you use pure JavaScript to open your link, and don't assign a href attribute, nothing will turn up in the status bar.
I don't know how much control you have over the chart html, but if it renders <a> tags there's not much you can do.
You could try running javascript after the chart is renderred to attach onclick event handlers manually to all <a> tags and set href = '#'.
I had a similar problem that led me to this thread. I figured I'd post my solution here rather than start a new thread about it.
I have a WordPress site with complex menus, and those menus had subheadings which the theme rendered as <a> links with empty href values. The site manager didn't want the bottom corner to display as a link if those were hovered over, since they didn't work has a link anyway.
<a href="#" class=" no_link" style="cursor: default;" onclick="Javascript: return false;">
I tried removing the "#" under href but it still showed the site's root url on hover.
Since the anchor tag's class list already included no_link as a class, I simply added the following jQuery to the global JavaScript file:
$("a.no_link").removeAttr("href");
Note that the intention was to simply remove a link's address on hover if it wasn't supposed to be a functional link anyway.
Refer this link here :
http://www.experts-exchange.com/Web_Development/Miscellaneous/Q_21278295.html
However , if the chart is not under your control, then this may not nbe the way
As suggested in the link :
Pink Floyd
You can also use jQuery to bind the mouseover event on these anchor links without editing individual <a>

Categories