News:

SMF 2.1.4 has been released! Take it for a spin! Read more.

Main Menu

Javascript help.

Started by GerryM, October 03, 2017, 03:07:15 AM

Previous topic - Next topic

GerryM

I am in the process of building a new listings site using Osclass with SMF bridged to it.

I have a problem that I cant figure out how to resolve.

By default, all external links from the site open in the same window, taking users off the site. There's no way I can find to change this, other than some javascript to force external links into a new window. Now, here lies the issue I have.

The site menu, when clicked to open sub-categories, is being opened in a new window as well, instead of opening a sub-menu.

Here's the code I'm using to force external links into a new window:


<script>
function ready(fn) {
  if (document.readyState != 'loading') {
    fn();
  } else if (document.addEventListener) {
    document.addEventListener('DOMContentLoaded', fn);
  } else {
    document.attachEvent('onreadystatechange', function() {
      if (document.readyState != 'loading')
        fn();
    });
  }
}

ready(function() {

  var website = window.location.hostname;

  var internalLinkRegex = new RegExp('^((((http:\\/\\/|https:\\/\\/)(www\\.)?)?'
                                     + website
                                     + ')|(localhost:\\d{4})|(\\/.*))(\\/.*)?$', '');

  var anchorEls = document.querySelectorAll('a');
  var anchorElsLength = anchorEls.length;

  for (var i = 0; i < anchorElsLength; i++) {
    var anchorEl = anchorEls[i];
    var href = anchorEl.getAttribute('href');

    if (!internalLinkRegex.test(href)) {
      anchorEl.setAttribute('target', '_blank');
    }
  }
});
</script>


My question is, is there a way, perhaps another line of code, that I could add to the script above to stop specific links being forced to a new window? The menu links are #dropdownMenu1, #dropdownMenu2, #dropdownMenu3, etc.

Any help on this would be appreciated. The Osclass forum has been no help whatsoever.

Gwenwyfar

Which links, exactly, are you trying to make open in a new window? By default all post links (from bbc code) should already do that. In any case you only need a simple attribute changed/added to the anchor's html to change this behavior, using JS is not really the answer.
"It is impossible to communicate with one that does not wish to communicate"

GerryM

The attribute for the links are dynamically generated, so very complex to change.

As it is, all links open in the same window, internal, and external.

I need to get external links opening in new windows. The code above does this. BUT, it also forces some internal links (eg. domain/#DropdownMenu1) to change to a new window.

My thought, is adding these internal links to "var exclude" white list, so they'll be ignored by the code. But I dont know how or where in the code above to add that.

Gwenwyfar

You could give this mod a try and see if it still works on 2.0.14: Internal Links Use Same Window
"It is impossible to communicate with one that does not wish to communicate"

GerryM

Thanks, but thats not what I need. I need to get this hard coded to the osclass part of the site. I'm just no good at javascript.

Gluz

Maybe a snippet of the code that generates the links could be useful and maybe there you can edit there and add the HTML to open in a new tab.

GerryM

Thanks for your input guys. I found a solution in some of the osclass code, and fixed it there.

Advertisement: