phppup Posted December 1 Share Posted December 1 The title says it all. I have a few instances for redirecting to another webpage with a click. Then I got to thinking (always leads to trouble), is there a difference or benefit between using: 1) a plain old HTML<a> 2)window.location in the <> tags 3) creating a JavaScript listener Quote Link to comment https://forums.phpfreaks.com/topic/326018-windowlocation-versus-a-anchor/ Share on other sites More sharing options...
requinix Posted December 2 Share Posted December 2 It makes no sense whatsoever to use Javascript in a place where a plain <a> link will work just fine. HTML first, CSS second, Javascript last. Quote Link to comment https://forums.phpfreaks.com/topic/326018-windowlocation-versus-a-anchor/#findComment-1644995 Share on other sites More sharing options...
maxxd Posted December 2 Share Posted December 2 I agree - only use JavaScript when it makes sense to use it; there's no need to invent a reason. If you're using a SPA framework (Vue, React, whatever is hot this week in JS land) then you've got a reason to use JS to redirect the browser. At the same point, those frameworks have the foundation in place that will handle redirects in the background. Quote Link to comment https://forums.phpfreaks.com/topic/326018-windowlocation-versus-a-anchor/#findComment-1645002 Share on other sites More sharing options...
phppup Posted December 2 Author Share Posted December 2 Sounds like good advice, but is there any real reasoning (other than that JS can be turned off)? Quote Link to comment https://forums.phpfreaks.com/topic/326018-windowlocation-versus-a-anchor/#findComment-1645014 Share on other sites More sharing options...
Barand Posted December 2 Share Posted December 2 Why waste your time writing JS code to replicate what the browser code already does (and compiled browser code will do the job far more efficiently than JS code) Quote Link to comment https://forums.phpfreaks.com/topic/326018-windowlocation-versus-a-anchor/#findComment-1645017 Share on other sites More sharing options...
maxxd Posted December 2 Share Posted December 2 ☝️ Quote Link to comment https://forums.phpfreaks.com/topic/326018-windowlocation-versus-a-anchor/#findComment-1645020 Share on other sites More sharing options...
gizmola Posted December 6 Share Posted December 6 I would agree unless there is some objective you have in mind that javascript would facilitate that a plain html link would not. However, since you are on the topic, one often neglected aspect of links is the rel attribute. Of particular interest are "nofollow", "ugc" and "canonical". There's some solid information about these on this page. Let me just say that they are very important to search engines and SEO. I'll start with "canonical". You might want to refer to this google page on the issue, and possible solutions. Let's say that you have a site where you have a shoe product, and the main way to get to that product page is to use the direct product url: <a href="/product/75">Rockport Men's Wingtip shoe</a> Let's also assume that you have categories and search in your site. You might have a category page for "shoes" like /category/shoes And when you can get to the same detail shoe page with a direct product link as in <a href="/category/shoes/75">Rockport Men's Wingtip shoe</a> The search engine crawls your site and unless you took steps to stop it, will crawl the categories pages and find your wingtips shoe page, and finding it in the /product catalog page using /product/75 page which is exactly the same content. Search engines can thus downgrade your resulting score, on the basis of the duplicate content. One way to fix this is to make sure that the product page link uses the rel="canonical" attribute. <a href="/product/75" rel="canonical">Rockport Men's Wingtip shoe</a> Quote Link to comment https://forums.phpfreaks.com/topic/326018-windowlocation-versus-a-anchor/#findComment-1645162 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.