How to share anchor links with angular components on angular 14? - html

I have a component menu which contains a few anchor tags. Each tag brings the user to that respective page section. I am trying to share the same anchor tag among the other components.
For example, I have two more HTML components called homepage.component.html and details.component.html. For each I call the menu.component.html by its selector. Both homepage and details html components have an id for the section I wanna scroll to. Here's how it looks like:
menu.component.html
Go to content
for both homepage.component.html and details.component.html
<div class="home-content" id="content"> Here comes more code </div>
It should work just like in a non-dynamic html project, however, when the anchor tag is clicked, the url redirects to '' (which is the first/default page) and then it shows the content for the first page, instead of the current componenet I am on.
I have tried creating a function where I get the current url and using the router.navigate, I pass the parameters indicating the fragment:
menu.component.ts
currentRoute: string
scrollToItem(){
this.currentRoute = this.router.url
this.router.navigate([this.currentRoute], {fragment: 'content'})
}
menu.component.html
<a (click)="scrollToItem()">Go to content</a>
However, this function adds the id #content to the url each time the anchor tag is clicked, redirecting the user to my 404 page.
I wanted to know if there is a way to use an anchor tag on the menu.componenet.html, while all the items that have "content" as their ids in different components are going to be displayed. Hopefully I made my question clear. If there is still questions about how the error occurs I can create and shate a stackblitz project. Thanks in advance :)

Related

Will an element with an href attribute always work when clicked

I am writing a suite of automated UI tests. I have a set of tests that verifies the links in a navbar work correctly, they take an annoying long time because it's loading 2 pages per test and there are many links in the nav bar. I am wondering if it is necessary to actually click the links?
One of the links would look like this, they're all basically the same, all contained inside a list of of <li> elements:
<a href="/projects/7d9162e5-e59c-452e-b9f5-684a2e0f2924/home" data-reactid=".0.2.0.0.0.$0.0">
<span class="icon icon-home" data-reactid=".0.2.0.0.0.$0.0.0"></span>
<span class="label" data-reactid=".0.2.0.0.0.$0.0.1">Home</span>
</a>
I could grab the content from the href attribute and request the page programmatically (don't load it in the browser) to assert that the href is correct and this would be significantly faster.
Is there any chance that an element could have an href attribute that points to the page as expected, but for whatever reason clicking on this element could be broken?
This might be the solution that you are looking for:
Link to Page
That code would append the href attribute as text to the body every time a link was clicked but not actually go to that link. The return false; part of that code prevents the browser from performing the default action for that link. That exact thing could be written like this:
$("a").click(function(e) {
$("body").append($(this).attr("href"));
e.preventDefault();
}
By taking the href content, you might risk that your automation passes test even though the navbar link does not work. It could be that navbar link was disabled by mistake, but as the link is still present in the DOM your automation will not capture it.
Just my 10 cents...

Using a tag in a dynamic url

I'm struggling to get the contact ref to work from different pages.
Basically for every product a different detail reference is provided (see 4867820 below).
I would like to create a button which links to the #contact id for every page on the website.
Basically if I put a link on the button including the reference, this works perfectly.
http://www.mywebsite/product/4867820#contact
However I am wondering if I can create something similar where the button links to the contact id on the same page without using the reference number.
http://www.mywebsite/product/this page#contact
So basically my question is how do I link to only #contact on this page without the reference numbers, so I can create 1 button on the page template.
Regards,
Assuming it will always point to the same page as the button:
a) If you are able to use anchor tags instead of buttons (and CSS-style them to look like buttons), you can simply point the anchor's href attribute to the section name.
It works like a relative link, just appending the href to current URL. Like so:
Contact Here
Or b) if you have to use only a <button> or <input> element specifically, you will need to use javascript to set the location in onclick handler of the button. For details on that see: Javascript inline onclick goto local anchor

How can I navigate using anchor tag in SAPUI5?

I know how to do using html and JavaScript
<h2 id="C4">Chapter 4</h2>
Jump to Chapter 4
This is what I am trying in SAPUI5. On click to Back to top link it should navigate to helpButton. This is not working for me.
<Button id="helpButton" icon ="sap-icon://sys-help" />
<Link text="Back to top"
press="#helpButton"/>
You can actually do this in UI5. A little differently than how you tried though.
The problem is that the UI5 ID is not the same as the HTML ID (which is what you want to use with the hash-link for the browser to jump there). Also, you cannot specify a URL inside the press "attribute" of the link. The press "attribute" is in fact an event (so you can only specify an event handler name).
So to be able to do what you want, you have to use the href property of the Link and fill it with the HTML ID of the target control. You can do this on the onAfterRendering hook of the view (that's when you are able to find the HTML ID of the target control):
onAfterRendering: function() {
var oRef = this.byId("target").getDomRef();
this.byId("link").setHref("#" + oRef.id);
}
You can find a working fiddle here: https://jsfiddle.net/93mx0yvt/26/.

Anchor with hash fragment is not navigating to matching id

I have an anchor with a hash fragment:
People
Farther down on my page I have a section with a matching ID:
<section id="people">...</section>
When the link is clicked, the URL updates, but the page does not move to the section.
You can try it out here, all the nav items have hash fragments, and each section on the page has a matching ID: https://react-test-don.herokuapp.com
Why does clicking my anchor tag not do anything?
For this I use
<a name="subject"></a> - At the position in the page where I want to go
Go to subject For the link to jump there
check out my page - you should be able to view source
Here
Just scroll down to where it says 'On this page' there are several links taking you to different sections of the page.

How to redirect to another page using Scala Template

I have two html pages: /algorithrms and /algorithms/add written in scala template. The route file contains following lines:
GET /algorithms controllers.Application.algorithms()
GET /algorithms/add controllers.Application.newAlgorithmForm()
I want to add a button in the page /algorithms and when I click that button, it simply redirects to the second page /algorithms/add. I know how to do this in JavaScript. I just want to call an action from the button click and then let the action redirects me to the landing page.
So I added the following code in the first page's html template:
#form(action=routes.Application.newAlgorithmForm()){
<input type="submit" value="Add">
}
It worked, but the landing url is: http://localhost:9000/algorithms/add?
I don't want that question mark. I want to know 1) what I did wrong to cause the question mark to generate and 2) how to remove it?
I do not know if you use Twitter bootstrap, but hyperlinks can look like buttons too, and the redirect to another page sounds to me like a plain hyperlink:
<a class="btn" href="#controllers.routes.Application.newAlgorithmForm()" >
#Messages("add.newAlgorithmForm")
</a>