Advertisement:

Author Topic: Javascript help.  (Read 1735 times)

Offline GerryM

  • Newbie
  • *
  • Posts: 7
Javascript help.
« on: October 03, 2017, 03:07:15 AM »
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:

Code: [Select]
<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.

Offline Gwenwyfar

  • Customization Team Apprentice
  • Full Member
  • ***
  • Posts: 588
  • Gender: Female
    • Gwenwyfar on GitHub
Re: Javascript help.
« Reply #1 on: October 03, 2017, 07:42:51 AM »
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.

Offline GerryM

  • Newbie
  • *
  • Posts: 7
Re: Javascript help.
« Reply #2 on: October 03, 2017, 08:01:32 AM »
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.

Offline Gwenwyfar

  • Customization Team Apprentice
  • Full Member
  • ***
  • Posts: 588
  • Gender: Female
    • Gwenwyfar on GitHub
Re: Javascript help.
« Reply #3 on: October 03, 2017, 08:39:44 AM »
You could give this mod a try and see if it still works on 2.0.14: Internal Links Use Same Window

Offline GerryM

  • Newbie
  • *
  • Posts: 7
Re: Javascript help.
« Reply #4 on: October 03, 2017, 10:15:55 AM »
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.

Offline Gluz

  • Native Language Support Specialist
  • Sr. Member
  • *
  • Posts: 944
  • Gender: Male
Re: Javascript help.
« Reply #5 on: October 04, 2017, 10:12:30 PM »
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.
Need help with PHP, HTML, CSS, Themes or MODs?

Quote
Think in spanish, habla en ingl├ęs.

Offline GerryM

  • Newbie
  • *
  • Posts: 7
Re: Javascript help.
« Reply #6 on: November 04, 2017, 09:05:35 AM »
Thanks for your input guys. I found a solution in some of the osclass code, and fixed it there.