1

I am trying to create a "random article" menu with Javascript at the bottom of blog posts.

I am wondering if there is a way to get the script to read the of the current article so I can omit that from the array and not have the article link to itself.

I get that I'll have to change the way the array data is stored, just need to know if I can make JS read the HTML tag.

Thanks!

//array is [<title>, <img src>]
var arts = [
["Santorini", "santo1_450h"], 
["Penang", "penang1"], 
["Porto", "Porto6_450h"], 
["Crete", "Crete5"], 
["Langkawi", "langkawi2"], 
["Singapore", "singapore1"]
];

var clone = [];

function shuffle(array) {
  //shuffles the array      

  return clone;
}

shuffle(arts);

function createRandArts() {
  //creates a bunch of HTML content
 }


createRandArts();
  • What do you mean by the current article in this context? In general yes you can find and remove something from an array. In this case it appear's that an array of object's is better suited than a 2D array. – Spencer Wieczorek Feb 06 '19 at 00:03
  • Could you give an example of your document title? – Keno Feb 06 '19 at 00:04
  • If you used jQuery, it'd be super clean --> `var title = $('title').html();` – Zak Feb 06 '19 at 00:09
  • Possible duplicate of [How to get the title of HTML page with JavaScript?](https://stackoverflow.com/questions/1057059/how-to-get-the-title-of-html-page-with-javascript) – Jon P Feb 06 '19 at 00:09
  • 3
    @Zac ..... cleaner than plain javascript? `var title = document.title` – Jon P Feb 06 '19 at 00:10
  • @Zak Just because you _can_ use jQuery doesn't mean you should. In this case it's completely unnecessary. – Spencer Wieczorek Feb 06 '19 at 00:12
  • @Zak Jquery in 2019? – Keno Feb 06 '19 at 00:15
  • @Spencer Wieczorek so this script runs in a bunch of different HTML files and I want to pull the of the current one, and yeah then use an array of objects and remove the object with {name:<title>}. Think my q was answered below though. Thanks! – snookumpiex Feb 06 '19 at 00:18

3 Answers3

1

You can use document.title to get the title of the current page and then loop through your array and remove it

Med Abida
  • 1,214
  • 11
  • 31
1

Here is how you read a title tag

var list = document.getElementsByTagName("TITLE")[0]
shobeurself
  • 128
  • 6
1

Yes you can use JS to read the document title, and then loop through your array, omitting that title if found.

Here's an example using an array of objects instead:

var arts = [
  { 
    title: "Santorini", 
    src: "santo1_450h"
  }, 
  {
    title: "Penang", 
    src: "penang1"
  }, 
  {
    title: "Porto", 
    src: "Porto6_450h"
  }, 
  {
    title: "Crete", 
    src: "Crete5"
  }, 
  {
    title: "Langkawi", 
    src: "langkawi2"
  }, 
  {
    title: "Singapore", 
    src: "singapore1"
  }
];

function shuffle(array) {   
  let clone = [];
  clone = arts.filter(e => e.title !== document.title);

  //shuffle clone here
  return clone;
}
Keno
  • 2,018
  • 1
  • 16
  • 26
  • Just be aware that fat arrow syntax is not supported in **any** version of IE: https://caniuse.com/#feat=arrow-functions . – Jon P Feb 06 '19 at 00:42
  • @Jon P Thankfully IE support is almost unnecessary unless you work for large corporations. – Keno Feb 06 '19 at 00:49
  • Perfect! this is what I had half written and was just looking up the .filter(...) part. Thank you! – snookumpiex Feb 06 '19 at 00:55
  • [IE currently has more users than Edge and is not that far behind Firefox.](https://caniuse.com/usage-table) Yes large corporations are more likely to have to support legacy browsers either for internal and external clients, but IE still represents a significant percentage of desktop users. – Jon P Feb 06 '19 at 01:03
  • @JonP I wouldn't consider < 3% to be that significant at all. It does depend on your actual userbase and/or internal software. But I won't throw it under the bus either way – Keno Feb 06 '19 at 01:10
  • 1
    @JonP Anyone who is using ES6 features should be using Babel anyways for production code. So it's not really an issue, in fact someone not using these features is pretty rare. – Spencer Wieczorek Feb 06 '19 at 03:45