1

I have a web page to download files using a button.But for some cases the files does not exit and download links are blank...So whenever the user clicks the link page just reloads.It would have been great if i could somehow disable the page reload and display an alert that says file not yet available..

This is the download link button code.row["slink"] gives the download link and may become empty sometime.

'<a href="'+row["slink"]+'"><button type="button" class="btn btn-info " data-row-id="' + row.id + '">Download</buttn></a> ';
Ndroid21
  • 400
  • 1
  • 8
  • 19
  • Possible duplicate of [Which "href" value should I use for JavaScript links, "#" or "javascript:void(0)"?](https://stackoverflow.com/questions/134845/which-href-value-should-i-use-for-javascript-links-or-javascriptvoid0) – lalithkumar Jul 04 '17 at 07:16
  • 1
    can't you just write an `if` statement above the line which produces this HTML which checked if the URL exists? if it doesn't just don't print the link – vsync Jul 04 '17 at 07:34
  • I need to print the link because multiple files are shown on the same page in a table.So if i dont print for one link that will look bad.. – Electronics For KTU Jul 04 '17 at 07:57

2 Answers2

3

Check if row['slink'] is present, else replace href with javascript:alert(...)(This won't do page reload for blank href, and show an alert):

var href = row["slink"].trim().length > 0 ? row["slink"] : "javascript:alert('File Not Yet Available')"


'<a href="'+href+'"><button type="button" class="btn btn-info " data-row-id="' + row.id + '">Download</buttn></a> ';
Abhi
  • 4,123
  • 6
  • 45
  • 77
1

Simple Solution

You can set target to blank:

<a ... ... target="_blank" > ... </a>

This will open it in a new tab instead of refreshing.

More Sophisticated Approach:

You can also use a conditional to build your anchor tag.

if (row["slink"] == "")
{
    var atag = '<a href="javascript:void(0)" onclick="alert(&quot;No File Available&quot;)"><button type="button" class="btn btn-info " data-row-id="' + row.id + '">Download</button></a> ';
}

This will give an alert if there is no file.

Community
  • 1
  • 1
Terrance00
  • 1,658
  • 1
  • 20
  • 29
  • This did help me but the alert box didnt show up but the other answer worked great.. – Electronics For KTU Jul 04 '17 at 07:51
  • It's the double quotes... silly of me. Will edit. Notice the " values in the alert - the same quotes can't be used within this tag builder code. The " html code will sort it out. – Terrance00 Jul 04 '17 at 07:53